0

I have built a bundle for Karaf with maven, and I included the org.postgresql dependency with

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

I also included the jar of the latest version to Karaf's lib/ext/ directory. The bundle is loaded correctly. However, at runtime, when the bundle tries to load the driver with:

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

I get the following error:

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

Is there anything more I have to add?

gc5
  • 9,468
  • 24
  • 90
  • 151
  • Possible duplicate of [How to include a jar of dependency in an OSGi bundle using maven bundle plugin?](http://stackoverflow.com/questions/16605746/how-to-include-a-jar-of-dependency-in-an-osgi-bundle-using-maven-bundle-plugin) – dcsohl May 28 '15 at 12:59
  • It seems to me a different problem, because I need karaf to see a library inside its `lib/ext` folder.. – gc5 May 28 '15 at 13:09
  • Are you sure? Both that question and your question revolve around making a database driver accessible to a bundle... that's why I thought that would be a good place for you to look. – dcsohl May 28 '15 at 13:12
  • Yes thanks, anyway I tried and it does not work. Indeed, I do not want to embed my dependency in the bundle but to make karaf load it independently.. – gc5 May 28 '15 at 13:22
  • In that case you should start by declaring the dependency with `provided` in your POM. Other than that I am not familiar with Karaf and do not know what needs to be done... – dcsohl May 28 '15 at 14:14

1 Answers1

1

I solved by embedding the dependency, using the corresponding tag (<Embed-Dependency>) in my 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>
gc5
  • 9,468
  • 24
  • 90
  • 151