我正在尝试运行一个JavaFx OSGi模块,但一直收到错误:
org.osgi.framework.BundleException: Unable to resolve OSGiDmHelloWorldProvider [7](R 7.0): missing requirement [OSGiDmHelloWorldProvider [7](R 7.0)] osgi.wiring.package; (osgi.wiring.package=javafx.application) Unresolved requirements: [[OSGiDmHelloWorldProvider [7](R 7.0)] osgi.wiring.package; (osgi.wiring.package=javafx.application)]
at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:4112)
at org.apache.felix.framework.Felix.startBundle(Felix.java:2118)
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:998)
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:984)
这是我加载和启动模块的方式:
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;
import java.io.File;
import java.util.*;
public class Launcher {
private static String[] libs = null;
private BundleContext context;
private Launcher() {
FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
Map<String, String> config = new HashMap<String, String>();
config.put("osgi.console", "");
config.put("osgi.clean", "true");
config.put("osgi.noShutdown", "true");
config.put("eclipse.ignoreApp", "true");
config.put("osgi.bundles.defaultStartLevel", "4");
config.put("osgi.configuration.area", "./configuration");
// automated bundles deployment
config.put("felix.fileinstall.dir", "./dropins");
config.put("felix.fileinstall.noInitialDelay", "true");
config.put("felix.fileinstall.start.level", "4");
config.put(Constants.FRAMEWORK_BOOTDELEGATION, "javafx.*,com.sun.javafx.*");
config.put(Constants.FRAMEWORK_BUNDLE_PARENT, Constants.FRAMEWORK_BUNDLE_PARENT_APP);
Framework framework = frameworkFactory.newFramework(config);
try {
framework.init();
framework.start();
} catch (BundleException e) {
e.printStackTrace();
}
context = framework.getBundleContext();
Bundle OSGiDmHelloWorldProvider = install("OSGiDmHelloWorldProvider");
try {
OSGiDmHelloWorldProvider.start();
} catch (BundleException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Launcher();
}
private String[] getLibs() {
if (libs == null) {
List<String> jarsList = new ArrayList<String>();
File pluginsDir = new File("libs");
System.out.println("PATHS : " + pluginsDir.getAbsolutePath());
for (String jar : pluginsDir.list()) {
jarsList.add(jar);
}
libs = jarsList.toArray(new String[jarsList.size()]);
}
return libs;
}
protected Bundle install(String name) {
String found = null;
for (String jar : getLibs()) {
if (jar.startsWith(name)) {
found = String.format("file:libs/%s", jar);
System.out.println(found);
break;
}
}
if (found == null) {
throw new RuntimeException(String.format("JAR for %s not found", name));
}
try {
return context.installBundle(found);
} catch (BundleException e) {
e.printStackTrace();
}
return null;
}
}
OSGiDmHelloWorldProvider
激活器类:
import com.bw.osgi.provider.able.HelloWorldService;
import com.bw.osgi.provider.impl.HelloWorldServiceImpl;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ProviderActivator implements BundleActivator {
Logger logger = LoggerFactory.getLogger(ProviderActivator.class);
Stage stage;
private ServiceRegistration registration;
BundleContext bundleContext;
@Override
public void start(BundleContext bundleContext) throws Exception {
registration = bundleContext.registerService(
HelloWorldService.class.getName(),
new HelloWorldServiceImpl(),
null);
Platform.runLater(new Runnable() {
@Override
public void run() {
stage = new Stage();
BorderPane pane = new BorderPane();
Scene scene = new Scene(pane, 400, 200);
pane.setCenter(new Label("This is a JavaFX Scene in a Stage"));
stage.setScene(scene);
stage.show();
}
});
System.out.println("STAGE CALLED !!!");
}
@Override
public void stop(BundleContext bundleContext) throws Exception {
registration.unregister();
logger.info("Set4Jfx Bundle: stop()");
Platform.runLater(new Runnable() {
@Override
public void run() {
stage.close();
}
});
}
}
如何在Maven
OSGi应用程序中加载使用JavaFx
的模块?
发布于 2017-02-01 17:44:54
该错误消息意味着您的捆绑包导入了包javafx.application
,没有捆绑包导出该包。因此,无法解析导入。
我注意到,在您的启动程序代码中,您尝试将引导委派设置为javafx.*
。这可能会允许从引导类路径加载类,如果您的包曾经运行过那么远的话,但是由于未解决的导入,它不能走那么远。
如果您打算从引导类加载器加载JavaFX类,那么应该从您自己的包中删除包导入。您还必须安排JavaFX类实际上由引导类加载器提供,因为它们通常只在扩展类加载器中可见。
但是,更好的解决方案是不进行导入,并安排从捆绑包中导出javafx.application
包。这可以使用Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA
从系统包中完成。
更新
你可能应该看看@Puce的Drombler FX工具/框架,因为听起来他已经完成了很多这样的设置任务。然而,我觉得他的回答并没有直接回答你的问题,为什么你的代码会失败。
发布于 2017-02-01 12:45:30
我已经发布了一些Drombler FX的第一个版本--基于OSGi和Maven的JavaFX的模块化应用程序框架(POM first)。
它负责初始化JavaFX和OSGi。
也许你会发现它很有用。应用程序框架是开源的,代码可以在GitHub上找到。
还有一个带有Getting Started踪迹的tutorial。
https://stackoverflow.com/questions/41979388
复制相似问题