java - Maven OSGi 项目可以编译,但无法运行

标签 java eclipse maven javafx osgi

我一直在尝试移植 Paul Bakker's (@paul-bakker) Making JavaFX better with OSGi : javafx-osgi-example到使用 Apache Felix Maven Bundle Plugin (BND) 的 Maven OSGi 项目。到目前为止,它编译没有任何错误,但我无法让它运行:

Starting OSGi Framework
Found declarative services implementation: file:/C:/Users/Rev/.m2/repository/org/apache/felix/org.apache.felix.scr/1.6.2/org.apache.felix.scr-1.6.2.jar
INFO : org.apache.felix.scr (1):  Version = 1.6.2
Bundle: org.apache.felix.framework
    Registered service: [org.osgi.service.resolver.Resolver]
    Registered service: [org.osgi.service.packageadmin.PackageAdmin]
    Registered service: [org.osgi.service.startlevel.StartLevel]
Bundle: org.apache.felix.scr
    Registered service: [org.apache.felix.scr.ScrService]
    Registered service: [org.osgi.service.cm.ManagedService]
    Registered service: [org.apache.felix.scr.impl.ScrGogoCommand]
DEBUG: Starting ComponentActorThread
Bundle: null
Bundle: null
Bundle: null

如您所见, bundle 从未启动。不会抛出任何错误。它只是无法启动。

为什么 bundle 无法启动?

项目来源

  1. Paul Bakker's (The original non-maven project) : javafx-osgi-example

  2. My maven implementation of Paul Bakker's javafx-osgi-example : JavaFX-Maven-Multi-Module-OSGi

从终端(Windows),mvn clean install 完美运行。

更新

我一直在尝试从 Eclipse 运行它,但没有成功:

Run -> Run as -> Java Application

<小时/>

Eclipse Java EE IDE for Web Developers.

Version: Mars.2 Release (4.5.2)
Build id: 20160218-0600

<小时/>

更新

我有课App在包 rev.dist 中的 dist 下这就是发射。它循环遍历 rev 下的所有目录,并启动名称与 resources/plugins.txt 下的名称匹配的所有 jar。 .

APP.java

package rev.dist;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.stream.Collectors;

import org.apache.commons.io.FilenameUtils;
import org.osgi.framework.Bundle;
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;

public class App {

    FrameworkFactory frameworkFactory;
    private Framework framework;

    private List<String> pluginsList = new ArrayList<>();

    private int addedPlugins;

    public static void main(String[] args) throws BundleException, URISyntaxException {
        App app = new App();
        app.initialize();
    }

    private void initialize() throws BundleException, URISyntaxException {
        this.plugins();

        Map<String, String> map = new HashMap<String, String>();

        // make sure the cache is cleaned
        map.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);

        map.put("ds.showtrace", "true");
        map.put("ds.showerrors", "true");

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

        System.out.println("Starting OSGi Framework");
        framework.init();

        loadScrBundle(framework);

        File baseDir = new File("../");
        String baseDirPath = baseDir.getAbsolutePath();

        File[] files = new File(baseDirPath).listFiles();

        this.showFiles(files);

        for (Bundle bundle : framework.getBundleContext().getBundles()) {
            bundle.start();
            System.out.println("Bundle: " + bundle.getSymbolicName());
            if (bundle.getRegisteredServices() != null) {
                for (ServiceReference<?> serviceReference : bundle.getRegisteredServices())
                    System.out.println("\tRegistered service: " + serviceReference);
            }
        }
    }

    public void showFiles(File[] files) throws BundleException {

        if (addedPlugins != pluginsList.size()) {
            System.out.println(":: " + pluginsList.size());
            addedPlugins--;
        }

        for (File file : files) {
            if (file.isDirectory()) {
                // System.out.println("Directory: " + file.getName());
                showFiles(file.listFiles()); // Calls same method again.
            } else {
                String[] bits = file.getName().split(".");
                if (bits.length > 0 && bits[bits.length - 1].equalsIgnoreCase("jar")) {
                    // framework.getBundleContext().installBundle(file.toURI().toString());
                }

                // String ext = FilenameUtils.getExtension(file.getAbsolutePath());

                String basename = FilenameUtils.getBaseName(file.getName());

                if (pluginsList.contains(basename)) {
                    framework.getBundleContext().installBundle(file.toURI().toString());
                    System.out.println("File: " + file.getName());

                    System.out.println("Base >>>>>>>>>>>>> : " + basename);

                    pluginsList.remove(basename);
                }
            }
        }
    }

    public void plugins() {
        File plugins = new File("src/main/resources/plugins.txt");
        String fileName = plugins.getAbsolutePath();

        try (BufferedReader br = Files.newBufferedReader(Paths.get(fileName))) {

            // br returns as stream and convert it into a List
            pluginsList = br.lines().collect(Collectors.toList());

        } catch (IOException e) {
            e.printStackTrace();
        }

        pluginsList.forEach(System.out::println);
        addedPlugins = pluginsList.size();
    }

    private void loadScrBundle(Framework framework) throws URISyntaxException, BundleException {
        URL url = getClass().getClassLoader().getResource("org/apache/felix/scr/ScrService.class");
        if (url == null)
            throw new RuntimeException("Could not find the class org.apache.felix.scr.ScrService");
        String jarPath = url.toURI().getSchemeSpecificPart().replaceAll("!.*", "");
        System.out.println("Found declarative services implementation: " + jarPath);
        framework.getBundleContext().installBundle(jarPath).start();
    }
}

最佳答案

我已经发布了 Drombler FX 的几个早期访问版本- JavaFX 的模块化应用程序框架。

它不是基于 Paul Bakker 的工作,但它也基于 OSGi 和 Maven(POM 优先)。也许你觉得它很有用。应用程序框架是开源的。

还有一个tutorialGetting Started页。

关于java - Maven OSGi 项目可以编译,但无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36263929/

相关文章:

java - 在批处理文件中包含 jar 文件时出错

java - 调用匿名类的方法

由于构建路径不正确,Java/Scala 导入无法正常工作?

eclipse - Gradle调试主类

Java Maven 组装插件 编译 fatal error : invalid target release 1. 14

java - Servlet [jsp] 的 Spring 微服务 : Servlet. service() 引发异常

java字符串替换和内存

java - 如何使用Stream过滤嵌套对象?

spring - Eclipse Tomcat 启动太快 - Tomcat 中的 Spring Web App 配置不起作用

android - 使用publishToMavenLocal创建远程Maven存储库的本地副本