2016-11-30 222 views
0

我想運行一個Maven項目,它使用數據庫池和數據庫上做一些東西。 其實我的代碼在Eclipse上正常工作,我得到的輸出。我的代碼如下:線程「主」java.lang.NoClassDefFoundError異常:org/apache/commons/dbcp2/BasicDataSource

package pack; 

import org.apache.commons.dbcp2.BasicDataSource; 

import java.sql.Connection; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 

public class App { 

    private final String CONNECTION_URL = "jdbc:derby:memory:test;create=true"; 

    private BasicDataSource ds; 

    //private Connection classicalWay() throws SQLException { 
    // return DriverManager.getConnection(CONNECTION_URL); 
    //} 

    public static void main(String[] args) { 
     final App app = new App(); 
     app.process(); 
    } 

    private Connection poolWay() throws SQLException { 
     if (ds == null) { 
      ds = new BasicDataSource(); 
      ds.setUrl(CONNECTION_URL); 
      ds.setMaxTotal(10); 
     } 
     return ds.getConnection(); 
    } 

    private Connection getConnection() throws SQLException { 
     return poolWay(); 
     //return classicalWay(); 
    } 

    private void createTable(final Statement stmt) throws SQLException { 
     stmt.execute("CREATE TABLE CUSTOMER (NAME VARCHAR(10))"); 
    } 

    private void insertRecord(final Statement stmt) throws SQLException { 
     stmt.execute("INSERT INTO CUSTOMER (NAME) VALUES ('James')"); 
    } 

    private void selectTable(Statement stmt) throws SQLException { 
     final ResultSet rs = stmt.executeQuery("SELECT * FROM CUSTOMER"); 
     while (rs.next()) 
      System.out.println(rs.getString(1)); 
    } 

    public void process() { 
     try { 
      final Connection con = getConnection(); 
      try { 
       final Statement stmt = con.createStatement(); 
       try { 
        createTable(stmt); 
        insertRecord(stmt); 
        selectTable(stmt); 
       } finally { 
        stmt.close(); 
       } 
      } finally { 
       con.close(); 
      } 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
    } 

}     

因此,我能夠在控制檯上獲得輸出「James」。另外我有所需的.jar文件。 我已經導入德比,derbyclient,公地dbcp2(版本2.1.1), 公共記錄(1.2版),公地POOL2,(版本2.4.2)和我的pom.xml如下:

  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
       <modelVersion>4.0.0</modelVersion> 

       <groupId>oguzbayral.trial.dummy</groupId> 
       <artifactId>Dummy-Project</artifactId> 
       <version>1.0-SNAPSHOT</version> 
       <packaging>jar</packaging> 

       <name>Dummy-Project</name> 
       <url>http://maven.apache.org</url> 

       <properties> 
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
       </properties> 

       <dependencies> 

        <dependency> 
         <groupId>junit</groupId> 
         <artifactId>junit</artifactId> 
         <version>3.8.2</version> 
         <scope>test</scope> 
        </dependency> 

        <dependency> 
         <grenter code here`oupId>org.apache.commons</groupId> 
         <artifactId>commons-dbcp2</artifactId> 
         <version>2.1</version> 
        </dependency> 

       </dependencies> 
       <build> 
        <plugins> 
         <plugin> 
          <groupId>org.codehaus.mojo</groupId> 
          <artifactId>exec-maven-plugin</artifactId> 
          <version>1.5.0</version> 
          <configuration> 
           <mainClass>list.Trial</mainClass> 
          </configuration> 
         </plugin> 
        </plugins> 
       </build> 
      </project> 

所以,我還添加了公地dbcp2作爲依賴 但是當我嘗試運行終端代碼(我在Mac上工作) 我得到下面的異常錯誤:

Oguz-Bayral:Dummy-Project OguzBayral$ java -cp target/Dummy-Project-1.0-SNAPSHOT.jar oguzbayral.trial.dummy.Apply 
       Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/dbcp2/BasicDataSource 
        at oguzbayral.trial.dummy.Apply.poolWay(Apply.java:19) 
        at oguzbayral.trial.dummy.Apply.getConnection(Apply.java:27) 
        at oguzbayral.trial.dummy.Apply.process(Apply.java:47) 
        at oguzbayral.trial.dummy.Apply.main(Apply.java:67) 
       Caused by: java.lang.ClassNotFoundException: org.apache.commons.dbcp2.BasicDataSource 
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
        at `enter code here`sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) 
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
        ... 4 more 

是不是很奇怪,我已經輸入了他們?有什麼問題,我該如何解決它?提前致謝。

+0

在終端中運行它時,必須將所有依賴項的jar包添加到classpath中,而不僅僅是程序本身的JAR。 – Jesper

+0

@Jesper我試過了,但沒有奏效。我已經將Jun​​it 3.8.1 jar添加到我的類路徑中,並且我確信其他jar文件的版本完全匹配。現在我有junit 3.8.1,commons-pool2 2.4.2,commons-dbcp2 2.1.1和commons logging 1.2在maven依賴關係中,我也將它們放在引用庫中。另外我還有一些其他的.jar文件,比如derby.jar,derbyLocale_esjar或者derbyclient.jar等。再次感謝! – 88yomc

+0

要明確:您必須將它們全部添加到命令行中作爲'-cp'選項的參數:'java -cp target/Dummy-Project-1.0-SNAPSHOT.jar:thislib.jar:thatlib.jar:anotherlib .jar ...' – Jesper

回答

0

似乎這個類不在你的類路徑中。導入一個類並不能保證在運行時你的類路徑中也有這個類。

相關問題