gpt4 book ai didi

以编程方式启动 OSGi 框架时出现 java.util.NoSuchElementException

转载 作者:行者123 更新时间:2023-12-04 04:45:31 25 4
gpt4 key购买 nike

我正在尝试从 Java 以编程方式启动 OSGi 框架(最好是 Equinox),然后加载任何包。

所以我正在经历这个article这说明了如何做到这一点。但我得到 java.util.NoSuchElementException 每次在下面的行每次

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 文件-
<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 在类路径中搜索您要求的接口(interface)——在本例中为 FrameworkFactory .要修复它,您需要在类路径上放置一个 OSGi 框架实现。

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

关于以编程方式启动 OSGi 框架时出现 java.util.NoSuchElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18244334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com