簡體   English   中英

無法將本地jar加載到我的jar中

[英]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文件不會打包到您的二進制文件中。 換句話說,罐子不在類路徑上。

  1. 嘗試使用provided的Maven -scope。 然后,odbc-jar必須位於JBoss的某個lib文件夾中,類似於Tomcat中的lib-folder。

  2. 您可以將odbc-jar install到本地Maven存儲庫中,然后將依賴項包含在default-scope中。

  3. 如果您的方案是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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM