Search code examples
javaosgiapache-felix

OSGI Activator class not found


I'm trying to make the simplest OSGI example in order to see if it works on a particular Java Virtual Machine. Currently I'm testing it with Open JDK 1.8.0.

I'm following this tutorial. I have the exact same files except that I removed the package statement from the .java file and also from the manifest (I just want to make it simpler).

So basically the manifest looks like that:

Bundle-Name: Service listener example
Bundle-Description: A bundle that displays messages at startup and when service events occur
Bundle-Vendor: Apache Felix
Bundle-Version: 1.0.0
Bundle-Activator: Activator
Import-Package: org.osgi.framework

I am able to generate the jar file. It's contents is the following:

" zip.vim version v27
" Browsing zipfile /home/cosmin/OSGI_Testing/osgi/test.jar
" Select a file with cursor and press ENTER

META-INF/
META-INF/MANIFEST.MF
build/Activator.class

I run the Apache Felix, I install the .jar file and when I start it throws that exception:

ERROR: Bundle [1] Error starting file:/home/cosmin/OSGI_Testing/osgi/. /test.jar (org.osgi.framework.BundleException: Not found: Activator)
java.lang.ClassNotFoundException: Activator not found by [1]
  at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1610)
  at org.apache.felix.framework.BundleWiringImpl.access$200(BundleWiringImpl.java:80)
  at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:2018)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  at org.apache.felix.framework.BundleWiringImpl.getClassByDelegation(BundleWiringImpl.java:1404)
  at org.apache.felix.framework.Felix.createBundleActivator(Felix.java:4505)
  at org.apache.felix.framework.Felix.activateBundle(Felix.java:2220)
  at org.apache.felix.framework.Felix.startBundle(Felix.java:2145)
  at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1372)
  at org.apache.felix.framework.FrameworkStartLevelImpl.run(FrameworkStartLevelImpl.java:308)

What is the problem?

Thank you respectfully.


Solution

  • There are several problems in this. The Activator class is in the wrong folder ... but that was already mentioned. Apart from this you should always use a package name in OSGi. Especially later when you want to share packages you have to make sure that the same package is not used in more than one jar.

    You seem to create the Manifest by hand. This is a very error prone process. I recommend to use a maven build and use either the maven-bundle-plugin or the bnd-maven-plugin to generate the Manifest. This is a lot safer.

    You can look into a tutorial I did some time ago. Maybe you need to strip it down for your case but you will see the maven build.

    Starting with an Activator for your first try is a good idea .. but you should then quickly switch to a dependency injection framework. For OSGi I recommend to look into declarative services.