进口;“外部”;嵌入式OSGi equinox框架中的java类

进口;“外部”;嵌入式OSGi equinox框架中的java类,osgi,equinox,Osgi,Equinox,我必须将Eclipse/EquinoxOSGi框架嵌入到现有的应用程序中 我找到了本教程,并尝试使用FRAMEWORK\u SYSTEMPACKAGES\u EXTRA Constans 所以我尝试通过系统包将一个简单的java类导出到运行框架中,但我从激活器包中收到以下错误 Exception in thread "main" org.osgi.framework.BundleException: The activator com.bundleactivator.Activator

我必须将Eclipse/EquinoxOSGi框架嵌入到现有的应用程序中

我找到了本教程,并尝试使用FRAMEWORK\u SYSTEMPACKAGES\u EXTRA Constans

所以我尝试通过系统包将一个简单的java类导出到运行框架中,但我从激活器包中收到以下错误

    Exception in thread "main" org.osgi.framework.BundleException: The activator com.bundleactivator.Activator for bundle com.bundleactivator is invalid
at org.eclipse.osgi.framework.internal.core.AbstractBundle.loadBundleActivator(AbstractBundle.java:156)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:751)
at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:370)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:284)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:276)
at RunNewEquinoxOsgi.main(RunNewEquinoxOsgi.java:74)
    Caused by: java.lang.Error: Unresolved compilation problem: 
The import example cannot be resolved

at com.bundleactivator.Activator.<init>(Activator.java:8)
at java.lang.J9VMInternals.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1505)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.loadBundleActivator(AbstractBundle.java:151)
... 5 more
我在其他java类中通过FrameworkFactory启动equinox

    import java.util.HashMap;
    import java.util.Map;
    import java.util.ServiceLoader;
    import org.osgi.framework.Bundle;
    import org.osgi.framework.BundleContext;
    import org.osgi.framework.BundleException;
    import org.osgi.framework.Constants;
    import org.osgi.framework.ServiceReference;
    import org.osgi.framework.launch.Framework;
    import org.osgi.framework.launch.FrameworkFactory;
    import example.osgi.*;

    FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
    Map<String, String> config = new HashMap<String, String>();
    config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
    "example.osgi;version=\"1.0.0\"");
    try {
        framework.start();
        System.out.println("Framework start");
    } catch (BundleException e) {
        e.printStackTrace();
    }

    BundleContext ctx = framework.getBundleContext();

    Bundle b3 = ctx.installBundle("file:C:\\equinoxtest\\ds.jar");
    System.out.println(b3.getBundleId() + " installed");
    Bundle b4 = ctx.installBundle("file:C:\\equinoxtest\\services.jar");
    System.out.println(b4.getBundleId() + " installed");
    Bundle b5 = ctx.installBundle("file:C:\\equinoxtest\\util.jar");
    System.out.println(b5.getBundleId() + " installed");
    Bundle b6 = ctx.installBundle("file:C:\\equinoxtest\\service.jar");
    System.out.println(b6.getBundleId() + " installed");
    Bundle b7 = ctx.installBundle("file:C:\\equinoxtest\\rule1.jar");
    System.out.println(b7.getBundleId() + " installed");
    Bundle b8 = ctx.installBundle("file:C:\\equinoxtest\\rule2.jar");
    System.out.println(b8.getBundleId() + " installed");
    Bundle b9 = ctx.installBundle("file:C:\\equinoxtest\\rule3.jar");
    System.out.println(b9.getBundleId() + " installed");
    Bundle b10 = ctx.installBundle("file:C:\\equinoxtest\\activator.jar");
    System.out.println(b10.getBundleId() + " installed");

    b3.start();
    b4.start();
    b5.start();
    b6.start();
    b7.start();
    b8.start();
    b9.start();
    b10.start(); // here i start the activator
在OSGi控制台中,我在系统包导出中看到这个包(example.OSGi),但是导入类(activator)抛出异常。我还应该做什么

    >osgi packages 0
    ...
    example.osgi; version="1.0.0"<org.eclipse.osgi_3.6.2.R36x_v20110210 [0]>
>osgi软件包0
...
例如osgi;version=“1.0.0”
此错误:

 java.lang.Error: Unresolved compilation problem
指示您正在使用Eclipse构建代码,但存在编译错误。您需要在运行代码之前对其进行编译


背景信息:Eclipse不使用
javac
命令进行编译,它有一个称为ECJ的内置Java编译器。此编译器是为IDE内部的增量使用而设计的,即使出现错误,它也会尽最大努力继续运行。因此,它仍然会生成一个有效的.class文件,但是,如果您实际尝试运行与类相关的内容,但在中出现错误,则会出现上述运行时错误。相比之下,
javac
在遇到编译错误时就会停止,并且不会生成可以运行的.class文件。

感谢您的快速响应。你的回答表明了另一个问题。那么,我如何在osgi捆绑包中使用来自“其他”应用程序的类呢?我尝试从FrameworkFactory运行osgi捆绑包?我认为它是在应用程序运行时导入的,使用额外的系统包和导入类就足够了。如何在编译之前使这些“额外”类可见,并在运行时避免出错?我的博客文章(您链接到的)只告诉您如何在运行时执行此操作。您似乎在询问如何在构建时执行此操作,但我无法回答这个问题,因为您没有说过任何关于构建环境的内容。例如,您是在Eclipse或PDE中设置“纯Java”项目,还是使用?您是使用Maven、ANT还是其他工具进行构建的?我使用Eclipse插件开发来创建捆绑包(activator、服务接口、服务实现)。框架的启动器和我试图通过system_packages_extra导出的“extra类”当然也是Eclipse中的(普通的)Java项目。当我在没有“额外类”的情况下运行它时,它工作得很好(我使用的是声明性服务)。我在Eclipse中通过导出->可部署插件和片段创建了捆绑包(.jar)。我将“extra class”包导入launcher类,这是正确的方法吗?希望了解PDE的人能帮助您,我已经多年没有使用它了。如果你对OSGi开发很认真,你可能想尽快抛弃PDE,使用Bndtools…非常感谢!我将尝试用Bndtools解决我的问题。我希望它能起作用。
    >osgi packages 0
    ...
    example.osgi; version="1.0.0"<org.eclipse.osgi_3.6.2.R36x_v20110210 [0]>
 java.lang.Error: Unresolved compilation problem