为什么我在启动服务时得到 "Activator not found"?

Why am I getting "Activator not found" when starting my service?

我为 OSGi Felix 框架测试编写了一个测试 class。 class如下:

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;
    }
}

}

以下是我的 manifest.mf 文件:

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

然后我使用以下 2 个命令编译 classes 并生成 bundle jar 文件。

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

然后我登录到 OSGi,当我使用以下命令启动服务时,

start file:/C:/Users/user/Desktop/assignment2/producer.jar

它给出了 BundleException:Not 找到的激活剂 class。异常如下:

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.access0(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

来自 [95]

为什么会出现该错误?请指教

我认为问题是您的 类 没有正确打包在您的 JAR 文件中。清单文件已正确配置,您在 jar 命令中使用正确的选项将其添加到您的 jar 文件中。

我建议您使用更高级的构建系统来构建您的项目,例如 Ant 或 Maven。这是 Ant 的示例 build.xml 文件。也许您可以尝试一下,看看它是否能解决您的问题。

<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>

希望对你有帮助, 蒂埃里