Must be a simple answer, but somehow it's eluding me today... I have an ImageIcon referred to in a package file. The JPEG it refers to is in the same directory as the class file. When I run the app, no problem, it picks up the image and uses it. But when I jar the whole thing, the URL simply does not work. I've put the jpeg in every possible path in the jar file, and added the classpath of the jar file to the run command, but still no go. What's different about relative URL's in jar files (w.r.t ImageIcon) as opposed to straight filesystem locations?
ClassLoader.getSystemResource() is a convenience method, equivalent to ClassLoader.getSystemClassLoader().getResource() as far as I know.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
posted 15 years ago
Originally posted by Eugene Kononov: Correct me if I am wrong.. Not sure. The code that I posted works for me, but I never examined it closely. Perhaps someone here can explain the difference.
sorry, my mistake. i was,by mistake,looking at getResource() and getResourceAsStream() which are non-static methods and so the post. Thank you, shankar.
The JPEG it refers to is in the same directory as the class file And are your class files in packages, with corresponding directories? Or is everything in the (nameless) default package? If an image is closely associated with a given class (or package, at least) then storing it in the same directory as the class file may make sense, but this creates some complications. If your class is pkg_a.pkg_b.Foo, then to retrieve an image which is located in pkg_a/pkg_b/image.jpg you probably need either
"I'm not back." - Bill Harding, Twister
posted 15 years ago
Thanks guy, your suggestion; URL imageURL = ClassLoader.getSystemResource(img); worked fine. I'm coming back to Java after a long swing through C++ XML, CSS2 and so on, where each idiom has a slightly different way of handling relative URL's and URI's. So when I came back to java, I started out using the simplest constructor, (ImageIcon(String filename)) and same idea with background images using the MediaTracker interface. But now that I'm back to javaland I guess I'll have to dig into Resource Bundles again and all that hairy URI stuff I thought I could avoid.