问题
I have an OSGi bundle that can also run in plain Java process. I need to be able to tell if the bundle was loaded in an OSGi system or not. How can I do that? If there is no OSGi-standard way of doing this, I will settle for Eclipse/Equinox-specific approach.
回答1:
If you don't have an activator, you can also try asking for your bundle:
Bundle b = org.osgi.framework.FrameworkUtil.getBundle(MyClass.this);
If it returns null, your class wasn't loaded by OSGi.
回答2:
Add Bundle-Activator to your MANIFEST.MF. If it gets instantiated, then your JAR is running in an OSGi container.
回答3:
You can check whether "this.getClass().getClassLoader() instanceof org.osgi.framework.BundleReference", which should only be true if you are running in an R4.2 OSGi framework.
来源:https://stackoverflow.com/questions/5879040/how-to-detect-if-running-in-osgi-container