Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/dbcp2/BasicDataSource
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/dbcp2/BasicDataSource
我正在尝试 运行 一个使用数据库池并在数据库上做一些事情的 Maven 项目。
实际上,我的代码在 Eclipse 上运行正常,并且我得到了输出。我的代码如下:
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 文件。
我已经导入了 derby、derbyclient、commons-dbcp2(版本 2.1.1),
commons logging(version 1.2), commons-pool2,(version 2.4.2) and my 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>
因此,我还添加了 commons-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
是不是很奇怪,我已经导入了?问题是什么,我该如何解决?非常感谢。
似乎 class 不在您的 class 路径中。导入 class 并不能保证您在运行时的 class 路径中也会有那个 class。
我正在尝试 运行 一个使用数据库池并在数据库上做一些事情的 Maven 项目。 实际上,我的代码在 Eclipse 上运行正常,并且我得到了输出。我的代码如下:
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 文件。 我已经导入了 derby、derbyclient、commons-dbcp2(版本 2.1.1), commons logging(version 1.2), commons-pool2,(version 2.4.2) and my 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>
因此,我还添加了 commons-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
是不是很奇怪,我已经导入了?问题是什么,我该如何解决?非常感谢。
似乎 class 不在您的 class 路径中。导入 class 并不能保证您在运行时的 class 路径中也会有那个 class。