我在 Eclipse 中有一个使用 JBoss 服务器的项目。我正在尝试更改我的数据库管理器以使用 C3P0 。但是,无论我尝试什么,我似乎都无法正确导入 jar 文件。
这是我收到的错误:
javax.ejb.EJBException: Unexpected Error
at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:157)
at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:213)
at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:284)
...
Caused by: java.lang.NoClassDefFoundError: com/mchange/v2/c3p0/ComboPooledDataSource
at com.softified.irw.common.DatabaseManager.<init>(DatabaseManager.java:24)
at com.softified.irw.common.DatabaseManager.getDataSource(DatabaseManager.java:35)
...
Caused by: java.lang.ClassNotFoundException: com.mchange.v2.c3p0.ComboPooledDataSource from [Module "deployment.irw-ear.ear:main" from Service Module Loader]
at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:191)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:361)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:333)
...
这是我尝试引用 jar 的代码的一部分:
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DatabaseManager {
private static ComboPooledDataSource cpds = null;
private static DataSource dataSource = null;
private static Connection connection = null;
private DatabaseManager(String clientName) {
try {
cpds = new ComboPooledDataSource();
cpds.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver
cpds.setJdbcUrl("java:jboss/datasources/" + clientName+"DS");
} catch (Exception e) {
// Handle error that it's not configured in JNDI.
throw new RuntimeException("Client "+clientName + " datasource configuration is missing in JNDI!", e);
}
}
我知道我可能没有正确使用 c3p0,但我现在只是想解决找不到类的问题。
以下是我导入 jar 所采取的步骤:
- 从此处下载 c3p0 库:http://sourceforge.net/projects/c3p0/?source=dlp
- 在引用该库的项目中创建一个文件夹“lib”,并将以下文件从 c3p0 下载中复制到其中:
- c3p0-0.9.5-pre8.jar
- mchange-commons-java-0.2.7.jar
- c3p0-oracle-thin-extras-0.9.5-pre8.jar(我知道我可能不需要这个,但我还是添加了它)
- 右键单击 Eclipse 中的每个 jar,然后点击“构建路径 > 添加到构建路径”。
- 所有三个 jar 现在都显示在我的“引用库”中
我的 .classpath 文件中包含以下内容:
<classpathentry kind="lib" path="lib/c3p0-0.9.5-pre8.jar" sourcepath="lib/c3p0-0.9.5-pre8-sources.jar"/>
<classpathentry kind="lib" path="lib/c3p0-oracle-thin-extras-0.9.5-pre8.jar"/>
<classpathentry kind="lib" path="lib/mchange-commons-java-0.2.7.jar" sourcepath="lib/mchange-commons-java-0.2.7-sources.jar"/>
当我尝试访问代码的相关部分时,我不断收到相同的错误消息。它找不到 c3p0 类文件。我究竟做错了什么?非常感谢任何帮助!
最佳答案
将所有提到的三个 jar 复制并放入 jboss 的 common lib 文件夹中并尝试,它会起作用。一旦它解决了这个问题,那么您可以检查类加载器问题。
关于java - 使用 C3P0 运行 JBoss 7 服务器的 Eclipse 项目出现 ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25169498/