I've bundled a JPG inside of my application's JAR file. I need to access it, preferably via a URL object from the code in the same JAR. But I've no idea how to construct the address. I'm guessing it would be a "file:///" but what comes after that?
Asked
Active
Viewed 300 times
0
-
possible duplicate of http://stackoverflow.com/questions/625687/java-getclassloader-getresource-driving-me-bonkers – Matthew Flaschen Apr 30 '10 at 03:11
-
It would only be a duplicate if I knew that ClassLoader or getResource had anything to do with it. Its content did help, as the comments below did, but I was needing the basis of the method vs. that question needing implementation details. – dacracot May 01 '10 at 14:37
1 Answers
2
You can use ClassLoader.getResource
, specifying a slash-delimited resource path.

Matthew Flaschen
- 278,309
- 50
- 514
- 539
-
2
-
also save yourself potential headaches and use 'absolute classpath paths', ie paths that start with a '/'. Otherwise the path is relative to the class that is calling the code, and can run into problems if the class is part of an inheritance hierarchy that spans packages. – MeBigFatGuy Apr 30 '10 at 04:42
-
not true - this answer states ClassLoader.getReosurce, which always treats resource names as relatve to the classpath root. – mdma May 01 '10 at 01:05