I wrote a test class for OSGi Felix framework testing. The class is as follows:
package tutorial.example2.service;
import java.util.Hashtable;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implements BundleActivator {
@Override
public void start(BundleContext context) throws Exception {
Hashtable<String, String> props = new Hashtable<String, String>();
props.put("Language", "English");
context.registerService(DictionaryService.class.getName(),
new DictionaryImpl(), props);
}
@Override
public void stop(BundleContext context) throws Exception {
// TODO Auto-generated method stub
}
private static class DictionaryImpl implements DictionaryService {
String[] m_dictionary = { "welcome", "to", "the", "osgi", "tutorial" };
public boolean checkWord(String word) {
word = word.toLowerCase();
for (int i = 0; i < m_dictionary.length; i++) {
if (m_dictionary[i].equals(word)) {
return true;
}
}
return false;
}
}
}
Following is my manifest.mf file:
Bundle-Name: English dictionary
Bundle-Description: A bundle that registers an English dictionary service
Bundle-Vendor: Apache Felix
Bundle-Version: 1.0.0
Bundle-Activator: tutorial.example2.service.Activator
Export-Package: tutorial.example2.service
Import-Package: org.osgi.framework
Then I used the following 2 commands to compile the classes and generate the bundle jar file.
javac -cp C:\Users\user\Desktop\felix-framework-4.0.3\bin\felix.jar *.java service\*.java
jar cfm producer.jar manifest_producer.mf -C C:\Users\kavin\Desktop \assignment2\producer
Then I logged into OSGi and when I start the service using the following command,
start file:/C:/Users/user/Desktop/assignment2/producer.jar
It gives a BundleException:Not Found activator class. Exception is as follows:
g! start file:/C:/Users/mayooranM/Desktop/TESTWS/Example2/src/tutorial/example2/
service/example.jar
org.osgi.framework.BundleException: Not found: tutorial.example2.service.Activat
or
at org.apache.felix.framework.Felix.createBundleActivator(Felix.java:417
5)
at org.apache.felix.framework.Felix.activateBundle(Felix.java:1972)
at org.apache.felix.framework.Felix.startBundle(Felix.java:1895)
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:944)
at org.apache.felix.gogo.command.Basic.start(Basic.java:729)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.felix.gogo.runtime.Reflective.invoke(Reflective.java:137)
at org.apache.felix.gogo.runtime.CommandProxy.execute(CommandProxy.java:
82)
at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:477)
at org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:4
03)
at org.apache.felix.gogo.runtime.Pipe.run(Pipe.java:108)
at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:183)
at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:120)
at org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessi
onImpl.java:89)
at org.apache.felix.gogo.shell.Console.run(Console.java:62)
at org.apache.felix.gogo.shell.Shell.console(Shell.java:203)
at org.apache.felix.gogo.shell.Shell.gosh(Shell.java:128)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.felix.gogo.runtime.Reflective.invoke(Reflective.java:137)
at org.apache.felix.gogo.runtime.CommandProxy.execute(CommandProxy.java:
82)
at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:477)
at org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:4
03)
at org.apache.felix.gogo.runtime.Pipe.run(Pipe.java:108)
at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:183)
at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:120)
at org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessi
onImpl.java:89)
at org.apache.felix.gogo.shell.Activator.run(Activator.java:75)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: tutorial.example2.service.Activator
not found by [95]
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDele
gation(BundleWiringImpl.java:1460)
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringIm
pl.java:72)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadCla
ss(BundleWiringImpl.java:1843)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.apache.felix.framework.BundleWiringImpl.getClassByDelegation(Bund
leWiringImpl.java:1317)
at org.apache.felix.framework.Felix.createBundleActivator(Felix.java:417
0)
... 33 more
java.lang.ClassNotFoundException: tutorial.example2.service.Activator not found
by [95]
Why am I getting that error? please advice.
I think that the problem is that your classes anre't correctly packaged within your JAR file. The MANIFEST file is correclty configured and you use the correct option in the jar command to add it within your jar file.
I recommend you to use more advanced build system to build your project like Ant or Maven. Here is a sample build.xml
file for Ant. Perhaps can you make a try with it to see if it fixes your problem.
<project name="My project" default="package" basedir=".">
<description>My project</description>
<property name="bin.dir" location="bin"/>
<property name="src.dir" location="src"/>
<property name="libs.dir" location="(the folder where my jar are)"/>
<path id="src">
<pathelement location="${src.dir}"/>
</path>
<target name="compile">
<mkdir dir="${bin.dir}"/>
<mkdir dir="${bin.dir}/META-INF"/>
<javac srcdir="${src.dir}" destdir="${bin.dir}" debug="true" debuglevel="lines,vars,source" source="1.7" target="1.7">
<classpath>
<fileset dir="${libs.dir}">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
<copy todir="${bin.dir}/META-INF">
<fileset dir="${src.dir}/META-INF"/>
</copy>
</target>
<target name="package" depends="compile">
<mkdir dir="${basedir}/target"/>
<delete file="${basedir}/target/myjarfile.jar" />
<zip destfile="${basedir}/target/myjarfile.jar"
basedir="${bin.dir}"/>
</target>
</project>
Hope it helps you, Thierry