从Maven运行Java程序时出现异常

29

我有一个小的Java程序,它连接到MySQL数据库并从中读取数据。我能够成功地使用java -cp运行它,但当我尝试使用mvn exec:java运行它时,程序完成后我会收到这个异常:

[WARNING] thread Thread[MySQL Statement Cancellation Timer,5,com.mycompany.mydivision.App] was interrupted but is still alive after waiting at least 15000msecs
[WARNING] thread Thread[MySQL Statement Cancellation Timer,5,com.mycompany.mydivision.App] will linger despite being asked to die via interruption
[WARNING] NOTE: 1 thread(s) did not finish despite being asked to  via interruption. This is not a problem with exec:java, it is a problem with the running code. Although not serious, it should be remedied.
[WARNING] Couldn't destroy threadgroup org.codehaus.mojo.exec.ExecJavaMojo$IsolatedThreadGroup[name=com.mycompany.mydivision.App,maxpri=10]
java.lang.IllegalThreadStateException
    at java.lang.ThreadGroup.destroy(ThreadGroup.java:775)
    at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:328)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:862)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:286)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:197)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)

为什么会发生这种情况,我该如何修复?以下是我的代码,以防需要:

为什么会发生这种情况,我该如何修复?以下是我的代码,以防需要:

public class App 
{

    public static void main( String[] args )
    {
        try (JdbcReader reader = new JdbcReader())
        {
            reader.test();
        }
        catch (SQLException ex) {
            // handle any errors
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
package com.mycompany.mydivision;

import com.vividsolutions.jts.geom.Geometry;

import java.io.Closeable;
import java.io.InputStream;
import java.sql.*;

/**
 * 
 */
public class JdbcReader implements Closeable{

    Connection conn;

    public JdbcReader() throws ClassNotFoundException, SQLException, IllegalAccessException, InstantiationException
    {
        // The newInstance() call is a work around for some
        // broken Java implementations
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        this.conn = DriverManager.getConnection("jdbc:mysql://localhost/mydb?user=guest");
    }

    /**
     * https://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-statements.html
     */
    public void test() throws Exception
    {
        Statement stmt = null;
        ResultSet rs = null;
        try {
            stmt = this.conn.createStatement();
            if (stmt.execute("SELECT * FROM mydb.my_table limit 20")) {
                rs = stmt.getResultSet();
                // Fetch each row from the result set
                while (rs.next()) {
                    String name = rs.getString("name");
                    String description = rs.getString("Descr");

                    System.out.printf("%s\t%s\n", name, description);
                }
            }
        }
        finally {
            // it is a good idea to release
            // resources in a finally{} block
            // in reverse-order of their creation
            // if they are no-longer needed

            if (rs != null) {
                try {
                    System.out.println("closing ResultSet");
                    rs.close();
                } catch (SQLException sqlEx) { } // ignore

                rs = null;
            }

            if (stmt != null) {
                try {
                    System.out.println("closing Statement");
                    stmt.close();
                } catch (SQLException sqlEx) { } // ignore

                stmt = null;
            }
        }
    }

    public void close()
    {
        if (conn != null)
        {
            try {
                System.out.println("closing connection");
                conn.close();
            }
            catch (SQLException ex) { } // ignore
            conn = null;
        }
    }
}

这是相关的内容,可能会有所帮助:https://dev59.com/EGYr5IYBdhLWcg3wssOP - the happy mamba
3个回答

41

在 exec Maven 插件中尝试这个

<configuration>
       <mainClass>com.test.Startup</mainClass>
       <cleanupDaemonThreads>false</cleanupDaemonThreads>
</configuration>

6
谢谢Francisco,这确实有效!我将其添加到了命令行中,而不是将其添加到pom.xml中,如下所示:mvn exec:java -Dexec.mainClass="com.mycompany.mydivision.App" -Dexec.cleanupDaemonThreads=false。 - morpheus
它能工作,没错,但 OP 的代码中有守护线程吗?为什么需要这样做呢? - LEQADA
1
从堆栈跟踪/错误信息中可以看出,是MySQL库创建了守护线程来监视语句的执行。由于Maven调用该类,因此它将隐式地创建这些守护线程。 - oligofren

12

我找到了一个稍微不同的解决方案。我遇到了类似的错误,猜测更改Maven配置可能会解决它,但我想先尝试修复错误本身;)。结果,我只需要在我的主方法末尾调用System.exit(status)就解决了问题。我猜exec插件需要显式调用System.exit(以及其他活动线程上的类似方法)才能正常工作。


4
我可以确认这一点。我们在main方法结束时使用System.exit(0)。 - karlihnos
我先尝试使用System.exit(0),但它没有起作用,而被接受的答案却可以。 - Raibaz
我也可以确认我的项目是这样的。 - ssavva05
这对我也有效。 - White_King
根据我收到的错误信息,我的四个(守护)线程“…尽管被要求通过中断来完成,但并未结束。”由于我的守护线程在被中断时不会关闭,这是有道理的。(这并不能解释为什么System.exit(0)没有起作用) - MiguelMunoz

6

在程序的最后一行明确使用System.exit(0);以解决此问题。


1
比被接受的答案好得多! - user1853859
即使还有其他步骤需要完成,它也会停止Maven进程。cleanupDaemonThreads方法在这种情况下也起作用。 - fillumina

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接