线程“主”java.lang.NoClassDefFoundError异常:org/apache/commons/dbcp2/BasicDataSource
问题描述:
我想运行一个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
是不是很奇怪,我已经输入了他们?有什么问题,我该如何解决它?提前致谢。
答
似乎这个类不在你的类路径中。导入一个类并不能保证在运行时你的类路径中也有这个类。
在终端中运行它时,必须将所有依赖项的jar包添加到classpath中,而不仅仅是程序本身的JAR。 – Jesper
@Jesper我试过了,但没有奏效。我已经将Junit 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
要明确:您必须将它们全部添加到命令行中作为'-cp'选项的参数:'java -cp target/Dummy-Project-1.0-SNAPSHOT.jar:thislib.jar:thatlib.jar:anotherlib .jar ...' – Jesper