尝试从 maven 运行 java 程序时出现异常

Exception while trying to run java program from maven

提问人:morpheus 提问时间:11/2/2015 更新时间:3/17/2023 访问量:7113

问:

我有一个小型 java 程序,它连接到 mysql 数据库并从中读取数据。我能够成功运行它,但是当我尝试运行它时,我在程序完成后出现此异常:java -cpmvn 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;
        }
    }
}
Java MySQL Maven

评论

0赞 the happy mamba 11/2/2015
这是相关的,可能会有所帮助:stackoverflow.com/questions/13471519/......

答:

41赞 Francisco Hernandez 11/2/2015 #1

在 exec maven 插件中尝试此操作

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

评论

6赞 morpheus 11/2/2015
谢谢弗朗西斯科,这确实有效!我没有将其添加到 pom 中.xml而是将其添加到命令行中,如下所示: mvn exec:java -Dexec.mainClass=“com.mycompany.mydivision.App” -Dexec.cleanupDaemonThreads=false
0赞 LEQADA 7/20/2020
它有效,是的,但是 OP 在代码中有守护进程线程的地方?为什么这是必要的?
1赞 oligofren 3/8/2021
@LEQADA 从stacktrace/errors中可以看出,是MySQL库创建了守护进程线程来监视语句的执行。由于 Maven 调用了该类,因此它将隐式创建这些守护进程线程。
12赞 gdawgrancid 8/3/2017 #2

我找到了一个略有不同的解决方案。我遇到了类似的错误,我猜更改 Maven 配置会修复它,但我想看看是否可以先;)修复错误本身。事实证明,我所需要的只是在我的主要方法的末尾调用,这解决了问题。我猜exec插件需要显式调用System.exit(以及其他活动线程上的类似调用)才能正常运行。System.exit(status)

评论

4赞 karlihnos 9/6/2017
我可以确认这一点。我们使用 System.exit(0) 和 main 的末尾。
0赞 Raibaz 4/26/2019
我首先尝试使用 System.exit(0),但它不起作用,而接受的答案却起作用了。
0赞 ssavva05 9/25/2020
我也可以为我的项目确认这一点。
0赞 White_King 2/10/2022
这也适用于我。
0赞 MiguelMunoz 7/19/2023
根据我收到的错误消息,我的四个(守护程序)线程“......尽管被要求打断,但还是没有完成。由于我的守护进程线程在中断时不会关闭,这是有道理的。(这并不能解释为什么 System.exit(0) 不起作用)
6赞 Girivasan 3/4/2021 #3

在程序的最后一行中显式使用来解决此问题。System.exit(0);

评论

1赞 user1853859 3/26/2021
比公认的答案要好得多!
0赞 fillumina 8/28/2021
即使有其他步骤需要完成,它也会停止 maven 进程。在这种情况下,cleanupDaemonThreads 方法也有效。