【问题标题】:Unable to load local jar inside my jar无法在我的 jar 中加载本地 jar
【发布时间】:2017-02-19 08:12:49
【问题描述】:

我正在努力解决这个问题,但找不到任何解决方案。 我正在使用 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>

但仍然收到相同的“未找到合适的驱动程序”消息。 任何帮助如何在我的 jar 中添加 ojdbc.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

【问题讨论】:

标签: java maven jboss6.x jbossfuse ojdbc


【解决方案1】:

我有同样的问题,我解决了它

将此存储库放入您的 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>

【讨论】:

    【解决方案2】:

    您可以使用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/ 文件夹并通过系统包导出它们。

    【讨论】:

      【解决方案3】:

      当您在pom.xml 中使用system-scoped 依赖项时,该依赖项的jar 文件不会打包到您的二进制文件中。换句话说,jar 不在类路径中。

      1. 尝试使用 Maven 的provided-范围。那么 odbc-jar 必须在 JBoss 的某个 lib 文件夹中,类似于 Tomcat 中的 lib 文件夹。

      2. 您可以将 install odbc-jar 放入本地 Maven 存储库,然后将依赖项包含在默认范围内。

      3. 1234563在与您不同的情况下,我能够使用以下 sn-p 部署和运行我的库。

      在您找到更好的东西之前,希望它可以作为一种解决方法:

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

      【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 2015-02-09
        • 2013-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-11
        • 1970-01-01
        相关资源
        最近更新 更多