0
如何創建可以雙擊打開的Java應用程序之類的IPOJO應用程序?像Java桌面應用程序嵌入Apache felix Ipojo
我有一些代碼:
//App.java
package app;
import app.testipojo.HelloComponent;
import java.util.HashMap;
import java.util.Map;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.framework.FrameworkFactory;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.Validate;
import org.apache.felix.main.AutoProcessor;
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;
@Component
@Instantiate
public class HelloComponentApp {
@Requires
HelloComponent c;
public HelloComponentApp() {
}
@Validate
public void start(){
c.test();
}
public static void main(String args[]) throws BundleException, InterruptedException{
FrameworkFactory ff = new FrameworkFactory();
Map<String,Object> config;
config = new HashMap<>();
config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,"org.osgi.service.log;version=1.3, org.apache.felix.ipojo.architecture;version=1.11.0, org.apache.felix.ipojo;version=1.11.0,"+
"org.osgi.service.cm;version=1.2,"+"app.testipojo;version=1.0.0.SNAPSHOT");
config.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
config.put(Constants.FRAMEWORK_STORAGE_CLEAN, "true");
Framework fwk = ff.newFramework(config);
fwk.start();
BundleContext context = fwk.getBundleContext();
String home_dir="file:/G:/HOW_TO_PRONOUNCE/install/jar/";
AutoProcessor.process(config, context);
Bundle bundle = context.installBundle(home_dir+"testipojo/target/testipojo-1.0-SNAPSHOT.jar");
bundle.start();
System.out.println("Started");
bundle.stop();
fwk.stop();
fwk.waitForStop(1000);
}
}
IPOJO捆綁開始
package app.testipojo;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
@Component
@Instantiate
public class HelloComponent {
public HelloComponent() {
}
public void test(){
System.out.println("Hello world!");
}
}
它運行沒有任何錯誤,但只打印 '開始'。它不打印'Hello world'。 請幫我解決這個問題。
我認爲你的應用程序中的start()方法未被執行。只有主要的方法被執行。 –