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([org.apache.felix.framework-4.4.1.jar:]
at org.apache.felix.framework.BundleWiringImpl.access$400([org.apache.felix.framework-4.4.1.jar:]
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(
at java.lang.ClassLoader.loadClass([:1.7.0_60-ea]
at java.lang.Class.forName0(Native Method)[:1.7.0_60-ea]
at java.lang.Class.forName([: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/

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

[ 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 (
[ 124] [Active     ] [            ] [   80] Apache ServiceMix :: Bundles :: javassist (3.12.1.GA_3)
[ 127] [Active     ] [            ] [   80] Apache ServiceMix :: Bundles :: json4s (
[ 128] [Active     ] [            ] [   80] Apache ServiceMix :: Bundles :: reflections (
[ 129] [Active     ] [            ] [   80] Apache ServiceMix :: Bundles :: swagger-annotations (
[ 130] [Active     ] [            ] [   80] Apache ServiceMix :: Bundles :: swagger-core_2.10 (
[ 131] [Active     ] [            ] [   80] Apache ServiceMix :: Bundles :: swagger-jaxrs_2.10 (
[ 145] [Resolved   ] [            ] [   80] Scala Standard Library (2.10.4.v20140209-180020-VFINAL-b66a39653b)
[ 148] [Installed  ] [            ] [   80] Apache ServiceMix :: Bundles :: c3p0 (
[ 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 -->


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
Tool: Bnd-1.50.0

The problem should go away if you done it correctly.

Original Post