4

I have a few jars which contain some resources with the same name. Lets say I have a.jar and b.jar and they both contain resource.xml. I know I can get a resource calling

getClass().getClassLoader().getResourceAsStream("resource.xml");

But AFAIK it may return a resource either from a.jar or b.jar. I want to pass the jar file name and get the resources from the specified jar.

user1745356
  • 4,462
  • 7
  • 42
  • 70
  • could you identify a specific class in a.jar or b.jar ? If yes, may be this answers could help : http://stackoverflow.com/questions/941754/how-to-get-a-path-to-a-resource-in-a-java-jar-file (the third answer) or this http://stackoverflow.com/questions/4505252/how-to-get-a-resource-in-another-jar – vincent Feb 03 '15 at 15:39

2 Answers2

3

Make sure that a.jar is before b.jar on the classpath.

If you cannot guarantee that you can do something like this

    ...
    Enumeration<URL> urls = getClass().getClassLoader().getResources("resource.xml");
    while(urls.hasMoreElements()) {
        URL url = urls.nextElement();
        if (url.getPath().contains("/a.jar")) {
            return url;
        }
    }
    return null;
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
-1

If they are in different packages (check your jars manifest) you can use packagename to access R class.

As an example:

getResources().getString(<com.mycompany.mypackage>.R.string.yourstring)
Juanjo Vega
  • 1,410
  • 1
  • 12
  • 20