Exception while trying to run java program from maven
我有一个小型 java 程序,它连接到 mysql 数据库并从中读取数据。我可以使用 java -cp 成功运行它,但是当我尝试使用 mvn exec:java 运行它时,程序完成后出现此异常:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
[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) |
为什么会发生这种情况,我该如何解决?这是我的代码以备不时之需:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
public class App
{ public static void main( String[] args ) import com.vividsolutions.jts.geom.Geometry; import java.io.Closeable; /** Connection conn; public JdbcReader() throws ClassNotFoundException, SQLException, IllegalAccessException, InstantiationException /** System.out.printf(“%s\\t%s\ if (rs != null) { rs = null; if (stmt != null) { stmt = null; public void close() |
- 这是相关的,可能会有所帮助:stackoverflow.com/questions/13471519/…
在 exec maven 插件中试试这个
1
2 3 4 |
<configuration>
<mainClass>com.test.Startup</mainClass> <cleanupDaemonThreads>false</cleanupDaemonThreads> </configuration> |
- 谢谢弗朗西斯科,这确实有效!我没有将其添加到 pom.xml 中,而是将其添加到命令行中,如下所示: mvn exec:java -Dexec.mainClass=”com.mycompany.mydivision.App” -Dexec.cleanupDaemonThreads=false
- 它可以工作,是的,但是 OP 在代码中哪里有守护线程?为什么这应该是必要的?
- @LEQADA您从堆栈跟踪/错误中看到,是 MySQL 库创建了守护线程来监视语句的执行。由于 Maven 调用该类,它将隐式创建这些守护线程。
我找到了一个稍微不同的解决方案。我有一个类似的错误,我猜更改 Maven 配置会修复它,但我想看看我是否可以先修复错误本身;)。事实证明,我所需要的只是在我的主要方法结束时调用 System.exit(status) 并解决了问题。我猜 exec 插件需要显式调用 System.exit(在其他活动线程上类似)才能正常运行。
- 我可以确认这一点。我们使用 System.exit(0) 和 main 的结尾。
- 我首先尝试使用 System.exit(0) 并没有用,而接受的答案却可以。
- 我也可以为我的项目确认这一点。
在程序的最后一行显式使用 System.exit(0); 来解决这个问题。
- 比接受的答案要好得多!
- 即使还有其他步骤要完成,它也会停止 maven 进程。 cleanupDaemonThreads 方法也适用于这种情况。
来源:https://www.codenong.com/33465161/