Search code examples
javaosgiapache-karafapache-felix

Where to find and install org.osgi.framework package?


I am trying to follow this tutorial:

apache felix tutorial #1

But I don't understand the directory structure. Where do I place the Activator.java file? Where, relative to the Activator.java file do I place the MANIFEST.MF?

In this tutorial when I try to compile the Activator.java file, I get errors that org.osgi.framework does not exist. Where can I get the org.osgi.framework package and where should it be installed? How does the javac build command know how to find the org.osgi.framework package?

I am looking for a tutorial that explains what Karaf/Felix framework is and how to get simple examples to work.

I have also tried to follow this tutorial: http://kevinboone.net/osgitest.html

but it is out of date and the building of the Tick bundle step fails. Specifically, the command:

javac -d target/tick/classes -classpath /path/to/karaf/lib/karaf.jar src/net/kevinboone/osgitest/tick/*.java

Does not make sense because there is not a file "karaf.jar" in my karaf installation that I downloaded from karaf download site.

In the manifest file, there is the line:

Import-Package: org.osgi.framework

but this package does not seem to exist in the karaf installation?


Solution

  • The felix tutorial is horribly outdated.

    If you want to learn OSGi I can recommend the Apache Karaf demo and tutorials as well as the enroute examples.

    Still to answer the question. You find the package org.osgi.framework in the osgi core spec jar and also in the felix jar. So one of these jars needs to be on the classpath during your compile.

    You should not create the manifest by hand. Instead use a plugin like the maven-bundle-plugin to create it. You can then place the Activator where ever you want and simply need to give the tool a hint where it is.

    Do not invest too much time into Activator and other low level OSGi APIs. They are not the way you should write code. Instead take a look at declarative services. It allows to wire your classes using annotations an takes care of the difficult low level tasks.

    The issue with the tutorial is that they use really low level commands like javac. Nowadays you should always use a build tool such as Maven or Gradle and declare the dependencies there. The tutorials and examples I linked all provide nice builds that you can also use for your later production code.