无法在我的罐子里装载本地罐子



我在这个问题上敲打我的头,但找不到任何解决方案。我正在使用 JBOSS-FUSE-6.1.0.REDHAT-379 服务器并在JBOSS_HOME/DEXPORY/FOLDER中部署我的Jar

我遇到的问题是,我运行应用程序时无法加载Oracle OJDBC Jar。我尝试在本地存储库中添加此ojdbc14.jar,然后在pom中添加依赖性:

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

它成功解决了导入问题,但是当我将jar部署在JBOSS中并运行我的应用程序时,它会出现一个错误:

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?

****更新****

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>

您可以使用Maven Bundle插件将罐子嵌入束中。这也将在您的清单中添加正确的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/文件夹中并通过系统捆绑包导出它们。

当您在 pom.xml中使用 system scoped依赖项时,依赖项的jar文件未包装到二进制文件中。换句话说,罐子不在类路径上。

  1. 尝试使用Maven的provided -Scope。然后,ODBC-JAR必须在JBOSS的某个LIB文件夹中,类似于Tomcat中的Lib-Folder。

  2. 您可以将odbc-jar install到您的本地Maven存储库中,然后将依赖项与默认符号一起包含。

  3. 如果您的方案是无法部署的,因为它将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;
}

您可以使用包装协议在karaf

中安装
osgi:install -s wrap:mvn:groupid/artifactid/version

最新更新