1

After hours I am giving up on debugging the following:

This works:

URL[] urls = ((URLClassLoader) MyClass.class.getClassLoader()).getURLs();
    URL myURL = null;
    for (URL url : urls) {
        if (url.getPath().endsWith("some.jar")) {
            myURL = url;
    }   
}
System.out.println(myURL);

returns

file:/C:/Users/Me/.m2/repository/path/to/some.jar

However, all of the following returns null:

MyClass.class.getClassLoader()).getResource("/some.jar");
MyClass.class.getClassLoader()).getResource("some.jar");
MyClass.class.getClassLoader()).getResource("/C:/Users/Me/.m2/repository/path/to/some.jar");
MyClass.class.getClassLoader()).getResource("/path/to/some.jar");

As you can see, I would like to get a jar of the user's maven repository by not adressing it absolutely, if possible. The jar is in the classpath, as shown by getURLs()
But how the heck do I have to address it in getResource() in order to get it?

Any help is appreciated!

Daniel K
  • 2,977
  • 2
  • 24
  • 23

2 Answers2

2

URLClassLoader.getURLs() returns the URLs to directories and JARs that are in the classpath. ClassLoader.getResource(String) is looking for the resource inside the classpath. So unless one of your JARs/directories in the classpath contains some.jar, this is expected to fail.

Put another way, if some.jar contains pkg/thingy.png, then getResource("pkg/thingy.png") would succeed.

Also note that getResource(String) returns a URL to the resource... a URL that you already have in myURL.

Dilum Ranatunga
  • 13,254
  • 3
  • 41
  • 52
  • OMG, I think, that's it, thanks! So using getResource() was totally wrong? Is finding the URL with getURLs(), like I did, the preferred way? – Daniel K Mar 28 '12 at 16:32
  • It depends on what you are trying to do. If you are trying to access a resource from *inside* one of the JARs in the classpath, then use `getResource(...)`. If you are trying to get alternate access to the JARs that comprise the classpath -- for example, to do an exhaustive scan of all available images -- use `getURLs(...)`. In my experience, only debugging code and framework code needs to resort to `getURLs`. – Dilum Ranatunga Mar 28 '12 at 16:36
  • Yeah, that's right, I only need that code during development. I expect all developers to have the jar file in their maven repository, which is in classpath. However, for the release I have to place it somewhere else (probably inside the release jar file). Thanks again for the explanation! – Daniel K Mar 28 '12 at 16:47
0

I suspect you want:

MyClass.class.getClassLoader().getResource("path/to/some.jar")

Note the lack of leading slash. That's assuming the "root" of your classloader is effectively repository. It's hard to tell just from the URL.

An alternative would be to use:

MyClass.class.getResource("/path/to/some.jar")

Note that this time there is a leading slash. Basically classloaders don't have any concept of a "relative" name, whereas classes do.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Unfortunately, both also do not work. But due to what Dilum Ranatunga pointed out in his answer, I will probably never be able to find it with getResource(). ;-) But thanks for your answer! – Daniel K Mar 28 '12 at 16:50