mss-2.0.0.FINAL-jboss-as-7.1.2.Final java.lang.ClassNotFoundException:com.mysql.jdbc.Driver

时间:2013-01-16 07:35:00

标签: mysql jboss

我在 Jboss 中使用.war文件创建了一个新应用程序我正在尝试在启动时连接MySQL DB(init方法), 我修改了 standalone-sip.xml 并添加了我的驱动程序: https://zorq.net/b/2011/07/12/adding-a-mysql-datasource-to-jboss-as-7/

在启动日志中,我可以看到正确加载驱动程序和数据源:

23:19:30,565 INFO  [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 28) JBAS010404: **Deploying non-JDBC-compliant driver class com.mysql.jdbc.Driver** (version 5.1)
23:19:30,748 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015876: Starting deployment of "mysql-connector-java-5.1.22-bin.jar"
23:19:30,750 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-3) JBAS010400: **Bound data source [java:jboss/datasources/opencall]**
23:19:30,933 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-4) JBAS010404: Deploying non-JDBC-compliant driver class com.mysql.jdbc.Driver (version 5.1)
23:19:30,954 INFO  [org.jboss.as.osgi] (MSC service thread 1-1) JBAS011907: Register module: Module "deployment.mysql-connector-java-5.1.22-bin.jar:main" from Service Module Loader

但我明白了:

23:18:50,959 ERROR [stderr] (MSC service thread 1-5) java.lang.ClassNotFoundException: com.mysql.jdbc.Driver from [Module "deployment.opencall-2.1.0-SNAPSHOT.war:main" from Service Module Loader]
23:18:50,959 ERROR [stderr] (MSC service thread 1-5)    at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:190)

之后我看到了:

23:18:51,700 INFO  [org.jboss.as.server] (Controller Boot Thread) JBAS018559: Deployed "mysql-connector-java-5.1.22-bin.jar"

我认为问题是我的应用程序是在加载mysql驱动程序之前启动的。但不确定在部署之前如何强制我的应用程序在启动期间使用mysql驱动程序。这是我的代码: 当我在没有Jboss的情况下运行它时它工作正常,只有当我部署war文件失败时。

try {

    Class.forName(dbClass).newInstance();

} catch (InstantiationException e) {
    logger.error("InstantiationException() Exception");
    e.printStackTrace();
} catch (IllegalAccessException e) {
    logger.error("IllegalAccessException() Exception");
    e.printStackTrace();
}
        if (dbPassword.equals(null))
    dbPassword="";
    Connection dbConnection = DriverManager.getConnection (dbUrl,dbUserName,dbPassword);
    dbConnection.close();

发现这个: 这是一个类似的问题: http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

1 个答案:

答案 0 :(得分:0)

<!-- mysql dependencies -->
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.23</version>
    </dependency>

在我的maven依赖项(pom.xml)中添加了mysql,然后重新构建