2015-11-01 6 views
10

मेरे पास एक छोटा जावा प्रोग्राम है जो एक 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; 
     } 
    } 
} 
+0

यह संबंधित है और इससे मदद मिल सकती है: http://stackoverflow.com/questions/13471519/running-daemon-with-exec-maven-plugin –

उत्तर

12

प्लगइन

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

धन्यवाद फ्रांसिस्को यह काम करता है! इसे pom.xml में जोड़ने के बजाय मैंने इसे कमांड लाइन में जोड़ा: mvn exec: java -Dexec.mainClass = "com.mycompany.mydivision.App" -Dexec.cleanupDaemonThreads = false – morpheus

+0

आपका स्वागत है! आपकी मदद करने में खुशी हुई: डी –

1

कार्यकारी Maven में इस प्रयास करें मैं एक अलग समाधान: यहाँ मामले में मेरी कोड यह आवश्यक है है। मुझे एक ही त्रुटि थी और मैं अनुमान लगा रहा हूं कि मेवेन कॉन्फ़िगरेशन इसे ठीक कर देगा, लेकिन मैं देखना चाहता था कि मैं पहले त्रुटि को ठीक कर सकता हूं;)। बाहर निकलता है, मुझे बस अपनी मुख्य विधि के अंत में System.exit (स्थिति) को कॉल करना था और समस्या को हल किया गया था। मुझे लगता है कि निष्पादन प्लगइन को ठीक से व्यवहार करने के लिए System.exit (और अन्य सक्रिय धागे पर समान) के लिए एक स्पष्ट कॉल की आवश्यकता है।

+1

मैं इसकी पुष्टि कर सकता हूं। हम System.exit (0) और मुख्य के अंत का उपयोग करते हैं। – karlihnos

संबंधित मुद्दे

 संबंधित मुद्दे