简体   繁体   中英

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

The problem I am having is that I am unable to load the oracle ojdbc jar when I run my application. I have tried adding this ojdbc14.jar in my local repository and then adding dependency in POM like:

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

java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@//ip:port/some_name

I have also tried adding ojdbc.jar like this in POM:

<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 ?

**** Update ****

Java Code:

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

   <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. In other words, the jar is not on the classpath.

  1. Try to use the provided -scope of Maven. Then the odbc-jar must be in some lib folder of JBoss, similar to the lib-folder like in Tomcat.

  2. You could to install the odbc-jar into your local Maven repository and then include the dependency with 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. 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 . This will also take care of adding correct OSGi headers in your manifest.

<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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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