문제 설명
OSGI 번들이 설치 및 시작되었지만 출력이 표시되지 않음 (OSGI Bundle Installed and Started but no visible output)
OSGI를 배우려고 합니다. (주로 번들의 동적 로드 및 언로드).
OSGi 포함 방법, 클래스 경로에 The Equinox OSGi 프레임워크 구현을 추가하고 게임을 시작했습니다.
코드는 다음과 같습니다.
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
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;
public class BundleManager {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto‑generated method stub
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();
BundleContext context = framework.getBundleContext();
List<Bundle> installedBundles = new LinkedList<Bundle>();
installedBundles.add(context.installBundle(
"file:C:/Users/student/Documents/eclipse/myPlugins/HellowService.jar"));
for (Bundle bundle : installedBundles) {
if (bundle.getHeaders().get(Constants.FRAGMENT_HOST) == null)
bundle.start();
}
System.out.println("done!!");
}
}
예 , 효과가있다. 오류가 전혀 없습니다. 그러나 경로:C:/Users/student/Documents/eclipse/myPlugins/HelloService.jar
에 jar 파일인 내가 설치한 번들은 시작 메소드에 "HelloWorld"를 포함합니다. 내 Eclipse 콘솔에 "HelloWold"가 표시되지 않습니다. 내가 왜 번들이 시작되었지만 해당 메시지가 표시되지 않습니까? 간단한 도움에 감사드립니다.
참고: HelloService.jar
는 내가 이전에 만든 플러그인 프로젝트로, 시작 부분에 "HelloWorld" 메시지를 추가하기 위해 해당 클래스 중 하나에 BundleActivator를 구현했습니다. 메소드를 만들고 마지막으로 C:/Users/student/Documents/eclipse/myPlugins/
편집: 여기 내가 설치하고 시작하는 번들의 Activator 클래스:
package com.javaworld.sample.service.impl;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import com.javaworld.sample.service.HelloService;
public class HelloServiceActivator implements BundleActivator {
ServiceRegistration helloServiceRegistration;
public void start(BundleContext context) throws Exception {
HelloServiceFactory helloServiceFactory = new HelloServiceFactory();
helloServiceRegistration =context.registerService(HelloService.class.getName(), helloServiceFactory, null);
System.out.println("Hello World!");
}
public void stop(BundleContext context) throws Exception {
helloServiceRegistration.unregister();
}
}
여기에 번들의 MANIFEST.MF 파일이 있습니다.
Manifest‑Version: 1.0
Bundle‑ManifestVersion: 2
Bundle‑Name: HelloService
Bundle‑SymbolicName: com.javaworld.sample.HelloService
Bundle‑Version: 1.0.0.qualifier
Bundle‑Activator: com.javaworld.sample.service.impl.HelloServiceActivator
Bundle‑Vendor: JAVAWORLD
Bundle‑RequiredExecutionEnvironment: JavaSE‑1.7
Import‑Package: org.osgi.framework;version="1.3.0"
Export‑Package: com.javaworld.sample.service
번들을 내보내는 방법은 다음을 마우스 오른쪽 버튼으로 클릭하는 것입니다. 번들 프로젝트‑>내보내기‑>실행 가능한 Jar 파일‑> 그런 다음 Launch Configuration을 BundleManager(번들을 설치하는 클래스임)로 선택합니다.
여전히 "Hello World!"가 표시되지 않습니다. 내 애플리케이션에서 번들을 시작할 때 메시지가 표시됩니다.
참조 솔루션
방법 1:
Your launcher does not wait for the OSGi Framework to stop. I would expect this program to start everything but then immediately shut down, because we reach the end of the main
method. Refer back to my tutorial where I show how to use the Framework.waitForStop
method.
Having said that, I would expect the output from your HelloWorld bundle to actually appear before the shutdown. So it seems likely there is an error in that bundle also. Perhaps you haven't declared the activator? I can only guess, because you haven't given any details.
방법 2:
It turned out that I was exporting the bundle incorrectly. That's because I tried to do it by myself. Here's how the bundle should be exported as a jar file:
Open the plugin export wizard File > Export... > Plug‑in Development >
Deployable plug‑ins and fragments .
Then select the bundle you want to export and the destination directory. Done!
You can now use the path of the jar file to install the bundle. In my case, it is:
installedBundles.add(context.installBundle(
"file:C:/Users/student/Documents/eclipse/myPlugins/plugins/com.javaworld.sample.HelloService_1.0.0.201307220322.jar"));
And for sure thanks to @Neil Bartlett
(by Traveling Salesman、Neil Bartlett、Traveling Salesman)