以编程方式启动OSGi框架时出现java.util.NoSuchElementException [英] java.util.NoSuchElementException while starting an OSGi framework programatically

查看:97
本文介绍了以编程方式启动OSGi框架时出现java.util.NoSuchElementException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以编程方式从Java启动OSGi框架(最好是Equinox),然后加载所有捆绑包.

I am trying to programatically start an OSGi framework (preferably Equinox) from Java and then load up any bundles.

所以我正在浏览文章,其中说了怎么做那.但是我每次都在下面的行中每次都得到 java.util.NoSuchElementException

So I was going through this article which says how to do that. But I am getting java.util.NoSuchElementException everytime at the below line everytime

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

下面是代码-

import java.io.File;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.logging.Logger;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;

public App() {      

    try {

        FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
        Map<String, String> config = new HashMap<String, String>();
        //TODO: add some config properties
        Framework framework = frameworkFactory.newFramework(config);
        framework.start();

            //...  some other code

        }
    }

我不确定我在这里做什么错?我有一个基于Maven的项目.以下是该项目的我的pom.xml文件-

I am not sure what wrong I am doing here? I am having a maven based project. Below is my pom.xml file for this project-

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <parent>
        <groupId>com.host.Stream</groupId>
        <artifactId>Stream-parent</artifactId>
        <version>2.0.0-SNAPSHOT</version>
        <relativePath>../Build/superpom</relativePath>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.host.domain.eye</groupId>
    <artifactId>eye</artifactId>
    <packaging>jar</packaging>
    <name>eye</name>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>                         <!-- Configuration of the archiver -->
                    <archive>                       <!-- Manifest specific configuration -->
                        <manifest>                  <!-- Classpath is added to the manifest of the created jar file. -->
                            <addClasspath>true</addClasspath>   <!-- Configures the classpath prefix. This configuration option is used to 
                                specify that all needed libraries are found under lib/ directory. -->
                            <classpathPrefix>lib/</classpathPrefix> <!-- Specifies the main class of the application -->
                            <mainClass>com.host.personalization.eye.App</mainClass>
                        </manifest>
                    </archive>
                    <includes>
                        <include>**/*.xml</include>
                        <include>**/*.class</include>
                    </includes>
                    <finalName>${project.artifactId}</finalName>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <outputDirectory>target/config/StreamConf</outputDirectory>
                    <resources>
                        <resource>
                            <directory>buildsrc/StreamConf</directory>
                            <includes>
                                <include>**</include>
                            </includes>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.google.code.maven-replacer-plugin</groupId>
                <artifactId>maven-replacer-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>replace</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <file>target/config/StreamConf/eyewiring.xml</file>
                    <regex>false</regex>
                    <replacements>
                        <replacement>
                            <token>dynamic_build_label_place_holder</token>
                            <value>${project.artifactId}-${project.version}-${buildNumber}</value>
                        </replacement>

                    </replacements>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <id>assembly</id>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>assembly.xml</descriptor>
                            </descriptors>
                            <finalName>${project.artifactId}-${project.version}-${buildNumber}</finalName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <executions>
                    <execution>
                        <id>generate-resources</id>
                        <goals>
                            <goal>manifest</goal>
                        </goals>

                        <configuration>
                        <manifestLocation>buildsrc/StreamConf/META-INF</manifestLocation>
                            <instructions>
                                <Bundle-Name>${project.name}</Bundle-Name>
                                <Bundle-SymbolicName>com.host.domain.eye</Bundle-SymbolicName>
                                <Import-Package>*,
                                    pics,
                                    links,
                                    javax.el;version="[2.0,3.0)";resolution:=optional,
                                    javax.inject;version="[1.0.0,2.0.0)",
                                    javax.servlet;version="[2.5,4.0)",
                                    javax.servlet.annotation;version="[3.0.0,4.0.0)",
                                    javax.servlet.http;version="[2.5,4.0)",
                                    javax.servlet.jsp;version="[2.0,3.0)",
                                    javax.servlet.jsp.el;version="[2.0,3.0)",
                                    org.aopalliance.aop;version="[1.0.0,2.0.0)",
                                    org.eclipse.virgo.web.dm;version="[2.1.0.RELEASE,3.0)",
                                    org.springframework.aop;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.aop.framework;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.aop.scope;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.beans;version="3.0.5.RELEASE",
                                    org.springframework.beans.annotation;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.beans.factory;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.beans.factory.annotation;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.beans.factory.config;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.context;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.context.annotation;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.context.config;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.stereotype;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.util;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.web.bind.annotation;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.web.context;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.web.context.request;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.web.context.support;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.web.servlet;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.web.servlet.config;version="[3.0.5.RELEASE,4.0)",
                                    org.springframework.web.servlet.view;version="[3.0.5.RELEASE,4.0)",</Import-Package>
                            </instructions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>

        <dependency>
            <groupId>com.host.Stream</groupId>
            <artifactId>Streamframework</artifactId>
            <version>2.0.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.host.external</groupId>
            <artifactId>ucirrus-db</artifactId>
            <version>0.7.3</version>
        </dependency>

        <dependency>
            <groupId>com.host.Stream</groupId>
            <artifactId>Streamcore</artifactId>
            <version>2.0.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>4.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.osgi</groupId>
            <artifactId>spring-osgi-core</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.osgi</groupId>
            <artifactId>spring-osgi-extender</artifactId>
            <version>1.2.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.osgi</groupId>
            <artifactId>spring-osgi-io</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>1.4.0</version>
        </dependency>
    </dependencies>
</project>

推荐答案

Service Loader在类路径中搜索所需的接口-在本例中为FrameworkFactory.要修复它,您需要在类路径上放置一个OSGi Framework实现.

Service Loader searches the classpath for the interface you have asked for -- in this case FrameworkFactory. To fix it you need to put an OSGi Framework implementation on the classpath.

例如,如果您选择Felix,请在启动应用程序时确保org.apache.felix.framework-<version>.jar存在于类路径中.

For example if you choose Felix, make sure that org.apache.felix.framework-<version>.jar is present on the classpath when you launch your application.

这篇关于以编程方式启动OSGi框架时出现java.util.NoSuchElementException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆