mr. Holiday
mr. Holiday

Reputation: 1800

OSGI plugin access the file in the resources.jar

I have to modify existing application to act as an OSGI plugin, I have stumbled on the following issue. This application depends on the xml file which is in the jar file, this application also loads this file during the runtime, and because of that in the OSGI environment it is not accessible. Any suggestion how I can fix this issue, so far I have tried following solution none have yield any positive results yet.

This is how initially it was loaded

InputStream templateInputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("resources/template.xml");

I tried to do it in the following way

URL url = null;
try {
  url = new URL("file:/lib/resources.jar");
} catch (MalformedURLException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
URL[] urls = {url};
URLClassLoader classLoader = new URLClassLoader(urls, DeclareMapFactory.class.getClassLoader());
Thread.currentThread().setContextClassLoader(classLoader);
InputStream templateInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("resources/template.xml");

This was also an attempt

InputStream templateInputStream = this.getClass().getClassLoader().getResourceAsStream("resources/template.xml");

Jar file is in the lib directory and is loaded via the MANIFEST.MF

Upvotes: 2

Views: 608

Answers (1)

Christian Schneider
Christian Schneider

Reputation: 19606

So lets assume template.xml is located at resources/template.xml in a jar resources.jar. This jar is inside your bundle and referenced using Bundle-ClassPath.

In this case the content of the resources jar should be added to the classpath of your bundle.

So when you are inside a class of the bundle you should be able to do:

this.getClass().getClassLoader().getResourceAsStream("resources/template.xml")

If that does not work then maybe you need to start the path with a "/" but I think it should work without.

Upvotes: 2

Related Questions