简体   繁体   English

无法将本地jar加载到我的jar中

[英]Unable to load local jar inside my jar

I am banging my head around this, but could not find any solution. 我正在为此努力,但找不到任何解决方案。 I am using jboss-fuse-6.1.0.redhat-379 server and deploying my jar inside Jboss_home/deploy/ folder 我正在使用jboss-fuse-6.1.0.redhat-379服务器并将我的jar部署在Jboss_home / deploy /文件夹中

The problem I am having is that I am unable to load the oracle ojdbc jar when I run my application. 我遇到的问题是我在运行应用程序时无法加载oracle ojdbc jar。 I have tried adding this ojdbc14.jar in my local repository and then adding dependency in POM like: 我尝试在本地存储库中添加此ojdbc14.jar,然后在POM中添加依赖项,例如:

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc14</artifactId>
    <version>10.2.0.4.0</version>
</dependency>

It successfully resolves the imports problem, but when I deploy my jar in Jboss and run my application, it gives an error that: 它成功解决了导入问题,但是当我在Jboss中部署jar并运行我的应用程序时,出现了以下错误:

java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@//ip:port/some_name java.sql.SQLException:找不到适用于jdbc:oracle:thin:@ // ip:port / some_name的驱动程序

I have also tried adding ojdbc.jar like this in POM: 我也尝试过在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>

but still getting the same "No suitable driver found" message. 但仍然收到相同的“找不到合适的驱动程序”消息。 Any help how can I add the ojdbc.jar inside my jar ? 任何帮助如何将ojdbc.jar添加到我的jar中?

**** Update **** ****更新****

Java Code: 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

i have same proplem and i solve it 我有同样的问题,我解决了

put this repository in your pom file 将此存储库放入您的pom文件中

   <repository>
       <id>codelds</id>
       <url>https://code.lds.org/nexus/content/groups/main-repo</url>
    </repository>

Then add your dependency 然后添加您的依赖

 <!-- ORACLE JDBC driver, need install yourself -->
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.1.0</version>
    </dependency>

When you use a system -scoped dependency in pom.xml , the jar file of the dependency is not packaged into your binary file. pom.xml使用system范围的依赖项时,该依赖项的jar文件不会打包到您的二进制文件中。 In other words, the jar is not on the classpath. 换句话说,罐子不在类路径上。

  1. Try to use the provided -scope of Maven. 尝试使用provided的Maven -scope。 Then the odbc-jar must be in some lib folder of JBoss, similar to the lib-folder like in Tomcat. 然后,odbc-jar必须位于JBoss的某个lib文件夹中,类似于Tomcat中的lib-folder。

  2. You could to install the odbc-jar into your local Maven repository and then include the dependency with default-scope. 您可以将odbc-jar install到本地Maven存储库中,然后将依赖项包含在default-scope中。

  3. If your scenario is, that the jar is not deployable, because it must be placed on a specific path in the file system, I would try to add the jar-File to classpath during runtime. 如果您的方案是jar不可部署,因为它must放置在文件系统中的特定路径上,那么我将尝试在运行时将jar-File添加到classpath中。 In another context than yours, I was able to deploy and run my lib with the following snippet. 在您以外的环境中,我可以使用以下代码片段部署和运行我的库。

Until you find something better, it hopefully serves as a work-around: 在找到更好的东西之前,希望它可以作为一种解决方法:

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;
}

You can embed a JAR inside your bundle using Maven Bundle Plugin . 您可以使用Maven Bundle Plugin将JAR嵌入到包中 This will also take care of adding correct OSGi headers in your manifest. 这还将有助于在清单中添加正确的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>

Anyway this is not a good approach. 无论如何,这不是一个好方法。 I would recommend using either JPA or adding Oracle JDBC drivers to the lib/ folder and exporting them through the system bundle. 我建议使用JPA或将Oracle JDBC驱动程序添加到lib/文件夹,然后通过系统捆绑包将其导出。

您可以使用包装协议在karaf中安装

osgi:install -s wrap:mvn:groupid/artifactid/version

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM