Search code examples
javaosgiclassloader

this.getClass().getResource("xyz.class") not returning DIR PATH in OSGi environment


My app is dependent on some SDK jars. This SDK makes extensive use of this.getClass().getResource("xyz.class") method in its APIs.

Now, i developed bundles based on these jars and running them in OSGi environment. But, In OSGi environment this.getClass().getResource("xyz.class") code line returns some bundle path to the xyz.class whereas it is expected that it should return the absolute DIR path with jar:// (as it does in non-OSGi env).

The question is how to get this work. I need the DIR PATH to the class that can be present in a dependent jar present in another bundle?


Solution

  • You can't. OSGi abstracts the location of the bundle contents. It is an abuse of the class loader API to assume all resource URLs are on the file system. The fact that is has been done in the past does not excuse the fact that it is violating the abstraction.

    That being said, the Eclipse Equinox framework implementation does provide an implementation specific way. See org.eclipse.core.runtime.FileLocator.toFileURL(URL).