我正在尝试使用OSGi-Bundle中的mysql-connector连接到MySQL数据库。不幸的是,当我试图在我的包中获取数据库驱动程序时,我总是得到NoClassDefFoundError。
我已经在这里待了两天,把头撞在墙上。我尝试了所有我发现的东西(我知道stackoverflow上已经有类似的问题了)但我现在完全迷失了。
该项目是一个gradle项目,我正在使用Intellij。这是bundle的build.gradle:
defaultTasks 'clean', 'build'
apply plugin: 'java'
apply plugin: 'osgi'
apply plugin: 'maven'
sourceCompatibility = 1.6
targetCompatibility = 1.6
version = '0.0.1'
def bundleSymbolicName = 'at.my.test.drivers.mysqldb'
def bundleName = 'Driver - MySql Database Drive'
repositories {
mavenCentral()
}
configurations {
embed
}
dependencies {
compile group: 'org.osgi', name: 'org.osgi.core', version: '4.3.1'
compile group: 'org.osgi', name: 'org.osgi.compendium', version: '4.3.1'
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.2'
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.35'
embed group: 'mysql', name: 'mysql-connector-java', version: '5.1.35'
}
jar {
into('lib') {
from configurations.embed
}
}
jar {
manifest {
version = project.version.replace('-','.');
symbolicName = bundleSymbolicName
name = bundleName
instruction 'Bundle-ClassPath', '.,lib/mysql-connector-java-5.1.35.jar'
instruction 'Service-Component', 'OSGI-INF/components.xml'
}
}
mysql-connector-java-5.1.35.jar文件由gradle正确下载,并作为模块的依赖项添加:
这就是我尝试创建数据库连接的方式:
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
sqlConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","usr","pwd");
}
catch(Exception e) {
e.printStackTrace();
logger.error("unable to create database connection");
}
当我尝试启动模块时,我只能得到这个例外:
java.sql.SQLException: java.lang.NoClassDefFoundError: javax/naming/RefAddr
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:937)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:872)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:904)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:894)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java
:
请帮助:(
答案 0 :(得分:0)
修改强>: 如果您下载 mysql-connector-java-5.1.35-bin.jar ,它应该是开箱即用的,因为它已经是OSGi-Bundle ...所以你可以跳过步骤1)和2)
1)使用JDBC驱动程序创建OSGi-Bundle(只需按照本教程操作: https://wiki.eclipse.org/Create_and_Export_MySQL_JDBC_driver_bundle)以下是这些步骤:
2)现在解压缩创建的JAR文件并打开META-INF文件夹中的MANIFEST.MF并添加以下 Export-Package 和 Import-Package 定义:
Export-Package: com.mysql.jdbc;version="5.1.35";uses:="com.mysql.jdbc.
log,javax.naming,javax.net.ssl,javax.xml.transform,org.xml.sax",com.m
ysql.jdbc.jdbc2.optional;version="5.1.35";uses:="com.mysql.jdbc,com.m
ysql.jdbc.log,javax.naming,javax.sql,javax.transaction.xa",com.mysql.
jdbc.log;version="5.1.35",com.mysql.jdbc.profiler;version="5.1.35";us
es:="com.mysql.jdbc",com.mysql.jdbc.util;version="5.1.35";uses:="com.
mysql.jdbc.log",com.mysql.jdbc.exceptions;version="5.1.35",com.mysql.
jdbc.exceptions.jdbc4;version="5.1.35";uses:="com.mysql.jdbc",com.mys
ql.jdbc.interceptors;version="5.1.35";uses:="com.mysql.jdbc",com.mysq
l.jdbc.integration.c3p0;version="5.1.35",com.mysql.jdbc.integration.j
boss;version="5.1.35",com.mysql.jdbc.configs;version="5.1.35",org.gjt
.mm.mysql;version="5.1.35"
Import-Package: javax.net,javax.net.ssl;version="[1.0.1, 2.0.0)";resol
ution:=optional,javax.xml.parsers, javax.xml.stream,javax.xml.transfo
rm,javax.xml.transform.dom,javax.xml.transform.sax,javax.xml.transfor
m.stax,javax.xml.transform.stream,org.w3c.dom,org.xml.sax,org.xml.sax
.helpers;resolution:=optional,javax.naming,javax.naming.spi,javax.sql
,javax.transaction.xa;version="[1.0.1, 2.0.0)";resolution:=optional,c
om.mchange.v2.c3p0;version="[0.9.1.2, 1.0.0)";resolution:=optional,or
g.jboss.resource.adapter.jdbc;resolution:=optional,org.jboss.resource
.adapter.jdbc.vendor;resolution:=optional
我的完整清单文件如下所示:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.5.0_22-b03 (Sun Microsystems Inc.)
Built-By: pb2user
Specification-Title: JDBC
Specification-Version: 4.0
Specification-Vendor: Oracle Corporation
Implementation-Title: MySQL Connector Java
Implementation-Version: 5.1.35
Implementation-Vendor-Id: com.mysql
Implementation-Vendor: Oracle
Bundle-Vendor: Oracle Corporation
Bundle-Classpath: .
Bundle-Version: 5.1.35
Bundle-Name: Oracle Corporation's JDBC Driver for MySQL
Bundle-ManifestVersion: 2
Bundle-SymbolicName: com.mysql.jdbc
Export-Package: com.mysql.jdbc;version="5.1.35";uses:="com.mysql.jdbc.
log,javax.naming,javax.net.ssl,javax.xml.transform,org.xml.sax",com.m
ysql.jdbc.jdbc2.optional;version="5.1.35";uses:="com.mysql.jdbc,com.m
ysql.jdbc.log,javax.naming,javax.sql,javax.transaction.xa",com.mysql.
jdbc.log;version="5.1.35",com.mysql.jdbc.profiler;version="5.1.35";us
es:="com.mysql.jdbc",com.mysql.jdbc.util;version="5.1.35";uses:="com.
mysql.jdbc.log",com.mysql.jdbc.exceptions;version="5.1.35",com.mysql.
jdbc.exceptions.jdbc4;version="5.1.35";uses:="com.mysql.jdbc",com.mys
ql.jdbc.interceptors;version="5.1.35";uses:="com.mysql.jdbc",com.mysq
l.jdbc.integration.c3p0;version="5.1.35",com.mysql.jdbc.integration.j
boss;version="5.1.35",com.mysql.jdbc.configs;version="5.1.35",org.gjt
.mm.mysql;version="5.1.35"
Import-Package: javax.net,javax.net.ssl;version="[1.0.1, 2.0.0)";resol
ution:=optional,javax.xml.parsers, javax.xml.stream,javax.xml.transfo
rm,javax.xml.transform.dom,javax.xml.transform.sax,javax.xml.transfor
m.stax,javax.xml.transform.stream,org.w3c.dom,org.xml.sax,org.xml.sax
.helpers;resolution:=optional,javax.naming,javax.naming.spi,javax.sql
,javax.transaction.xa;version="[1.0.1, 2.0.0)";resolution:=optional,c
om.mchange.v2.c3p0;version="[0.9.1.2, 1.0.0)";resolution:=optional,or
g.jboss.resource.adapter.jdbc;resolution:=optional,org.jboss.resource
.adapter.jdbc.vendor;resolution:=optional
导出的软件包可以被其他软件包使用(这就是重点)。保存清单文件后,压缩文件扩展名并将文件扩展名更改为.jar并放入。将jar文件放入Felix OSGi环境的bundle文件夹(或任何你使用的),重要的是bundle安装并可用:
3)之后,您可以在已创建的任何其他包中导入com.mysql.jdbc包,只需在清单中导入包:
Import-Package: com.mysql.jdbc;version="5.1.35"
或者,如果您使用的是gradle,请在build.gradle中添加以下内容:
jar {
manifest {
version = project.version.replace('-','.');
symbolicName = bundleSymbolicName
name = bundleName
instruction 'Bundle-ClassPath', '.'
instruction 'Import-Package', 'com.mysql.jdbc;version="5.1.35"'
instruction 'Service-Component', 'OSGI-INF/components.xml'
}
}
4)最后你可以使用ist:
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
sqlConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","usr","pwd");
}
catch(Exception e) {
e.printStackTrace();
logger.error("unable to create database connection");
}
希望这有助于某人,我花了两天时间弄清楚这些东西是如何运作的^^
答案 1 :(得分:0)
将系统从Jboss5.1迁移到Wildfly 10时,我也遇到了这个错误
分析后,我们发现依赖项<module name="javax.api"/>
应该添加到受影响的module.xml
<强> Module.xml 强>
<module xmlns="urn:jboss:module:1.3" name="com.sql.mysql">
<resources>
<resource-root path="mysql-connector-java-5.1.20.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="sun.jdk"/>
</dependencies>
</module>
请参阅网址https://developer.jboss.org/thread/197723以获得类似的解决方案