OSGi got FrameworkFactory could not be instantiate

2019-01-29 08:44发布

问题:

I am trying to use OSGi and maven to develop an standard alone application.

I have the following osgi dependency in pom.xml

<dependency>
    <groupId>org.osgi</groupId>
    <artifactId>org.osgi.core</artifactId>
    <version>6.0.0</version>
</dependency>

I tried the following code

import org.osgi.framework.launch.FrameworkFactory;

FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();

and got this error message

Exception in thread "main" java.util.ServiceConfigurationError: org.osgi.framework.launch.FrameworkFactory: Provider org.osgi.framework.launch.FrameworkFactory could not be instantiated
at java.util.ServiceLoader.fail(ServiceLoader.java:232)
at java.util.ServiceLoader.access$100(ServiceLoader.java:185)
at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:384)
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
at com.scottfu.ci.test.OSGiTest.main(OSGiTest.java:19)
Caused by: java.lang.InstantiationException:    org.osgi.framework.launch.FrameworkFactory
at java.lang.Class.newInstance(Class.java:427)
at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:380)
... 3 more
Caused by: java.lang.NoSuchMethodException: org.osgi.framework.launch.FrameworkFactory.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.newInstance(Class.java:412)
... 4 more

what is wrong?

回答1:

The dependency you added is the OSGi Core API.

You need an implementation like equinox, felix or knopflerfish.

E.g.:

Instead of adding the dependency you mentioned, add

<dependency>
    <groupId>org.apache.felix</groupId>
    <artifactId>org.apache.felix.main</artifactId>
    <version>5.6.2</version>
</dependency>


标签: eclipse osgi