Red Hat
Sep 14, 2015
by Christina Lin

I have bumped into ClassNotFound Exception problem several times when I need to add Database driver or migrate non-OSGi libraries into my JBoss Fuse applications.


Could not load driverClass org.postgresql.Driver


java.lang.ClassNotFoundException: org.postgresql.Driver not found by org.apache.servicemix.bundles.c3p0 [135]
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1556)[org.apache.felix.framework-4.4.1.jar:]
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:77)[org.apache.felix.framework-4.4.1.jar:]
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1993)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)[:1.7.0_60-ea]
at java.lang.Class.forName0(Native Method)[:1.7.0_60-ea]
at java.lang.Class.forName(Class.java:190)[:1.7.0_60-ea]

The problem is mostly due to the class loading algorithm in OSGi. As each module need to specified what package it needs setting these packages in "Import-Package" inside META-INF/MANIFEST.MF of the bundle. But sometime your application load the driver dynamically in runtime, or the 3rd party library did not specified the import in their settings.

What I normally do when I am in the middle of development is to temporary resolve this problem in the JBoss Fuse Karaf container, for instance this example, I am using the c3p0 pool in servicemix, but it needs to dynamically lookup the postgresql database. What I will do is to install both servicemix c3p0 bundle and postgresql bundle

osgi:install mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.c3p0/0.9.1.2_1

Bundle ID: 148

osgi:install wrap:mvn:postgresql/postgresql/9.0-801.jdbc4

Bundle ID: 148

then do a osgi:list to list all available bundles, make sure they are all active

osgi:list
[ 100] [Active     ] [            ] [   80] Fabric8 :: Web (1.2.0.redhat-133)
[ 102] [Active     ] [            ] [   80] Bean Validation API (1.1.0.Final)
[ 123] [Active     ] [            ] [   80] Apache ServiceMix :: Bundles :: jackson-module-scala_2.10 (2.1.5.2)
[ 124] [Active     ] [            ] [   80] Apache ServiceMix :: Bundles :: javassist (3.12.1.GA_3)
[ 127] [Active     ] [            ] [   80] Apache ServiceMix :: Bundles :: json4s (3.2.4.1)
[ 128] [Active     ] [            ] [   80] Apache ServiceMix :: Bundles :: reflections (0.9.8.1)
[ 129] [Active     ] [            ] [   80] Apache ServiceMix :: Bundles :: swagger-annotations (1.3.2.1)
[ 130] [Active     ] [            ] [   80] Apache ServiceMix :: Bundles :: swagger-core_2.10 (1.3.2.1)
[ 131] [Active     ] [            ] [   80] Apache ServiceMix :: Bundles :: swagger-jaxrs_2.10 (1.3.2.3)
[ 145] [Resolved   ] [            ] [   80] Scala Standard Library (2.10.4.v20140209-180020-VFINAL-b66a39653b)
[ 148] [Installed  ] [            ] [   80] Apache ServiceMix :: Bundles :: c3p0 (0.9.1.2_1)
[ 149] [Installed  ] [            ] [   80] wrap_mvn_postgresql_postgresql_9.0-801.jdbc4 (0)

and then use dynamical loading for servicemix c3p0 to look up the postgresql

dev:dynamic-import 148
Enabling dynamic imports on bundle org.apache.servicemix.bundles.c3p0 [148]

But this is not a good idea when you want to package the application, I used the dynamical import when developing because I am the only person running the container, and the dependency may change rapidly when I am developing, but this is not the best way to do in OSGi, instead of a strict modularized system, it will search the public exported packages to find a match instead of the careful normal calculation. Not a good practice. That's when I want to deploy my application into production, is I will create a fragment bundle. There is no big secret about this  "fragment bundle", all you have to do is to specify a Fragment Host. And all the content in your fragment bundle will be made available to that host. From my example, all I have to do it to create an empty bundle, and have my servicemix c3p0 bundle as the fragment host and import postgresql host.

Fragment-Host: org.apache.servicemix.bundles.c3p0
Import-Package: org.postgresql 

To be more specific, create a JBoss Fuse blueprint project, remove all the unnecessary blueprint.xml or java classes (Basically an empty project), Then in your pom.xml, find the maven plugin, add your fragmented Host and package you need in Import-package tag.

      <!-- to generate the MANIFEST-FILE of the bundle -->
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <version>2.3.7</version>
        <extensions>true</extensions>
        <executions>
          <execution>
           <id>bundle-manifest</id>
           <phase>process-classes</phase>
           <goals>
               <goal>manifest</goal>
           </goals>
          </execution>
        </executions>
        <configuration>
          <instructions>
            <Bundle-SymbolicName>demojobfrag</Bundle-SymbolicName>
            <Private-Package>org.blogdemo.demojobfrag.*</Private-Package>
            <Fragment-Host>org.apache.servicemix.bundles.c3p0</Fragment-Host>
            <Import-Package>org.postgresql,*</Import-Package>
          </instructions>
        </configuration>

      </plugin>

And then deploy this bundle first along with your application. This is my MANIFEST.MF look like after packaging it.                

Manifest-Version: 1.0
Bnd-LastModified: 1441500854071
Build-Jdk: 1.7.0_60-ea
Built-By: christina
Bundle-ManifestVersion: 2
Bundle-Name: Camel Blueprint Route
Bundle-SymbolicName: demojobfrag
Bundle-Version: 1.0.0.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
Export-Package: org.blogdemo.demojobfrag;version="1.0.0.SNAPSHOT"
Fragment-Host: org.apache.servicemix.bundles.c3p0
Import-Package: org.osgi.service.blueprint;version="[1.0.0,2.0.0)",org.p
 ostgresql
Tool: Bnd-1.50.0

The problem should go away if you done it correctly.

Original Post