您應該使用org.eclipse.equinox.app.IApplication
接口,該接口允許您從start()
方法返回結果,然後將該結果作爲Java進程的退出代碼返回。如果你不想使用此API,下面的代碼,展示的Equinox本身如何控制Java進程的退出代碼:
import org.eclipse.osgi.service.environment.EnvironmentInfo;
private static EnvironmentInfo getEnvironmentInfo() {
BundleContext bc = Activator.getContext();
if (bc == null)
return null;
ServiceReference infoRef = bc.getServiceReference(EnvironmentInfo.class.getName());
if (infoRef == null)
return null;
EnvironmentInfo envInfo = (EnvironmentInfo) bc.getService(infoRef);
if (envInfo == null)
return null;
bc.ungetService(infoRef);
return envInfo;
}
public static void setExitCode(int exitCode) {
String key = "eclipse.exitcode";
String value = Integer.toString(exitCode); // the exit code
EnvironmentInfo envInfo = getEnvironmentInfo();
if (envInfo != null)
envInfo.setProperty(key, value);
else
System.getProperties().setProperty(key, value);
}
上面的代碼不採取一對一的,但它給了理念。
謝謝Danail,我做了一個有點類似的實現,設置系統屬性,監聽它並進一步傳播退出代碼 – javaresearcher