Karaf / Maven - 在运行时找不到类

时间:2015-05-28 12:43:25

标签: java postgresql maven jdbc apache-karaf

我已经使用maven为Karaf构建了一个包,我将org.postgresql依赖包含在

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.4-1201-jdbc41</version>
</dependency>

我还将最新版本的jar包含在Karaf的lib/ext/目录中。 捆绑包正确加载。但是,在运行时,捆绑包尝试使用以下命令加载驱动程序时

Class.forName("org.postgresql.Driver");

我收到以下错误:

2015-05-28 14:36:07,218 | ERROR | qtp1270203840-47 | MyBundleServlet               | 124 - MyBundle - 1.0.0.SNAPSHOT | org.postgresql.Driver not found by MyBundle [124]
java.lang.ClassNotFoundException: org.postgresql.Driver not found by MyBundle [124]
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1532)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)[:1.7.0_75]
at java.lang.Class.forName0(Native Method)[:1.7.0_75]
at java.lang.Class.forName(Class.java:191)[:1.7.0_75]
...

我还有什么需要补充的吗?

1 个答案:

答案 0 :(得分:1)

我通过在<Embed-Dependency>中使用相应的标记(pom.xml)嵌入依赖项来解决。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                    <Import-Package>*;resolution:=optional,!org.postgresql</Import-Package>
                    <Embed-Dependency>postgresql</Embed-Dependency>
                </instructions>
            </configuration>
        </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <skipTests>${skipTests}</skipTests>
            </configuration>
          </plugin>
    </plugins>
</build>