Taking an example from Neil Bartlett's book "OSGi in Practice", I copied the following code which can start, stop, and check for bundles in a directory, and which is supposed to print messages on bundle start and stop:
package tutorial;
import java.io.File;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
public class HelloUpdaterActivator implements BundleActivator {
// ...
private final Thread thread = new Thread(new BundleUpdater());
private volatile BundleContext context;
@Override
public void start(BundleContext context) throws Exception {
System.out.println("Hello World");
this.context = context;
thread.start();
}
@Override
public void stop(BundleContext context) throws Exception {
System.out.println("Goodbye World");
thread.interrupt();
}
protected Bundle findBundleByLocation(String location) {
// ... Not relevant to lack of any print statements in OSGi console.
}
private class BundleUpdater implements Runnable {
@Override
public void run() {
// ... Not relevant to lack of any print statements in OSGi console.
}
}
^ I put it in a file called HelloUpdaterActivator.java and put that java file in a folder called "tutorial" along with org.eclipse.osgi_3.5.1.R35x_v20090827.jar (The jar file for OSGi that came with an old version of the Eclipse Equinox implementation of OSGi).
I successfully compiled the tutorial java file with:
javac -cp ".:org.eclipse.osgi_3.5.1.R35x_v20090827.jar" HelloUpdaterActivator.java
Then I took all the class files that were generated and put them into a jar like so:
jar cf HelloUpdaterActivator.jar ./HelloUpdaterActivator.class ./HelloUpdaterActivator\$BundleUpdater.class ./HelloUpdaterActivator\$1.class
Finally, I made a generic sample MANIFEST.MF file for the jar, like so:
Manifest-Version: 1.0
Created-By: 1.7.0_79 (Oracle Corporation)
Bundle−Name: OSGi Bundle
Bundle−Symbolic Name: example1
Bundle−Version: 1.0.1
Bundle−Required Execution Environment: J2SE−1.6
Now that the jar was ready, I opened up OSGi in the terminal with its own little console, like so:
java -jar ./org.esgi_3.5.1.R35x_v20090827.jar -console -configuration runtime
The OSGi console looked like this:
osgi>
Into the OSGi console I typed:
osgi> install file:HelloUpdaterActivator.jar
Bundle id is 1
osgi> ss
Framework is launched.
id State Bundle
0 ACTIVE org.eclipse.osgi_3.5.1.R35x_v20090827
1 INSTALLED unknown_0.0.0 [1]
osgi> start 1
osgi> ss
Framework is launched.
id State Bundle
0 ACTIVE org.eclipse.osgi_3.5.1.R35x_v20090827
1 ACTIVE unknown_0.0.0 [1]
osgi> stop 1
osgi> ss
Framework is launched.
id State Bundle
0 ACTIVE org.eclipse.osgi_3.5.1.R35x_v20090827
1 RESOLVED unknown_0.0.0 [1]
But even though it says that the jar file I made is active, "Hello World" never prints on start and "Goodbye World" never prints on stop. Why won't my OSGi tutorial print "Hello World" on bundle start or "Goodbye world" on bundle stop?
UPDATE
I got it working by changing my MANIFEST.MF file to:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: OSGi_Bundle - Activator
Bundle-Version: 1.0.0.SNAPSHOT
Bundle-Activator: Activator
Bundle-ClassPath: .
Bundle-Description: Activator
Bundle-SymbolicName: activator
Import-Package: org.osgi.framework
You are missing a Bundle-Activator
header so it knows to call that class' methods.