[英]Unable to load local jar inside my jar
我正在为此努力,但找不到任何解决方案。 我正在使用jboss-fuse-6.1.0.redhat-379服务器并将我的jar部署在Jboss_home / deploy /文件夹中
我遇到的问题是我在运行应用程序时无法加载oracle ojdbc jar。 我尝试在本地存储库中添加此ojdbc14.jar,然后在POM中添加依赖项,例如:
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
它成功解决了导入问题,但是当我在Jboss中部署jar并运行我的应用程序时,出现了以下错误:
java.sql.SQLException:找不到适用于jdbc:oracle:thin:@ // ip:port / some_name的驱动程序
我也尝试过在POM中添加ojdbc.jar:
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/ojdbc14-10.2.0.4.0.jar</systemPath>
</dependency>
但仍然收到相同的“找不到合适的驱动程序”消息。 任何帮助如何将ojdbc.jar添加到我的jar中?
****更新****
Java代码:
try
{
// File CP_file = new File("/home/path/to/ojdbc14.jar");
// DBFactory dbMethod = new DBFactory();
// dbMethod.addJarToClasspath(CP_file);
Class.forName ("oracle.jdbc.OracleDriver");
String dbURL = "jdbc:oracle:thin:@//ip:port/name";
String userID = "userid";
String password = "pass";
// dbMethod.isJarOnClassPath(CP_file);
Connection dbConnection=DriverManager.getConnection(dbURL,userID,password);
// getting exception on above line
我有同样的问题,我解决了
将此存储库放入您的pom文件中
<repository>
<id>codelds</id>
<url>https://code.lds.org/nexus/content/groups/main-repo</url>
</repository>
然后添加您的依赖
<!-- ORACLE JDBC driver, need install yourself -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>
在pom.xml
使用system
范围的依赖项时,该依赖项的jar文件不会打包到您的二进制文件中。 换句话说,罐子不在类路径上。
尝试使用provided
的Maven -scope。 然后,odbc-jar必须位于JBoss的某个lib文件夹中,类似于Tomcat中的lib-folder。
您可以将odbc-jar install
到本地Maven存储库中,然后将依赖项包含在default-scope中。
如果您的方案是jar不可部署,因为它must
放置在文件系统中的特定路径上,那么我将尝试在运行时将jar-File添加到classpath中。 在您以外的环境中,我可以使用以下代码片段部署和运行我的库。
在找到更好的东西之前,希望它可以作为一种解决方法:
private boolean addJarToClasspath( File jarFile )
{
try
{
URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class<?> urlClass = URLClassLoader.class;
Method method = urlClass.getDeclaredMethod( "addURL", new Class<?>[] { URL.class } );
method.setAccessible( true );
method.invoke( urlClassLoader, new Object[] { jarFile.toURI().toURL() } );
System.out.println( jarFile.getAbsolutePath() + " dynamically added to classpath" );
return true;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
}
private boolean isJarOnClassPath( File jarFile )
{
URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
URL[] urls = urlClassLoader.getURLs();
for ( URL url : urls )
{
File file = new File( url.getFile() );
if ( file.getPath().endsWith( jarFile.getName() ) )
{
System.out.println( jarFile.getAbsolutePath() + " is on classpath" );
return true;
}
}
return false;
}
您可以使用Maven Bundle Plugin将JAR嵌入到包中 。 这还将有助于在清单中添加正确的OSGi标头。
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Embed-Dependency>ojdbc6</Embed-Dependency>
</instructions>
</configuration>
</plugin>
无论如何,这不是一个好方法。 我建议使用JPA或将Oracle JDBC驱动程序添加到lib/
文件夹,然后通过系统捆绑包将其导出。
您可以使用包装协议在karaf中安装
osgi:install -s wrap:mvn:groupid/artifactid/version
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.