java - 如何启动 JavaFx OSGi 包

标签 java maven intellij-idea javafx osgi

我正在尝试运行 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 的模块?

最佳答案

错误消息意味着您的 bundle 导入了包javafx.application,并且没有 bundle 导出该包。因此无法解决导入问题。

我注意到,在您的启动器代码中,您尝试将 bootdelegation 设置为 javafx.*。如果您的包曾经运行过,那么可能允许从启动类路径加载该类,但是由于未解析的导入,它无法运行到那么远。

如果您打算从引导类加载器加载 JavaFX 类,那么您应该从您自己的包中删除包导入。您还必须安排 JavaFX 类实际由引导类加载器提供,因为据我所知,它们通常只能从扩展类加载器中看到。

但是,更好的解决方案是保留导入,并安排从 bundle 中导出 javafx.application 包。这可以使用 Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA 从系统 bundle 中完成。

更新

您可能应该看看 @Puce 的 Drombler FX 工具/框架,因为听起来他已经完成了很多这样的设置任务。但是我觉得他的回答并没有直接解决您关于代码失败原因的问题。

关于java - 如何启动 JavaFx OSGi 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41979388/

相关文章:

java - Runtime.getRuntime().exec error=13 权限被拒绝

java - 使用startResolutionForResult()时,不会在fragment中调用onActivityResult();

java - Junit 在法语字符串断言上失败

maven - 在 maven 中解压任意 tar.gz 文件

java - Maven 无法解析类

java - 游戏框架-JAVA : Get Post Data

java - 优化字符串和嵌套搜索

angularjs - CreateProcess error=193,%1 不是有效的 Win32 应用程序 -> [帮助 1]

java - 无法将正则表达式参数添加到 split 函数

intellij-idea - Intellij + Ajc + Lombok/Mapstruct