wood burning stoves 2.0*
The moose likes Java in General and the fly likes relative URL in jar files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "relative URL in jar files" Watch "relative URL in jar files" New topic
Author

relative URL in jar files

Peter Kryszkiewicz
Greenhorn

Joined: Oct 25, 2003
Posts: 6
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?
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Where is the image? If it is part of a jar file, you need to load it from there, somethinng like this:
shankar vembu
Ranch Hand

Joined: May 10, 2001
Posts: 309
Originally posted by Eugene Kononov:


SHould read

Correct me if I am wrong..
Shankar.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
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.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
shankar vembu
Ranch Hand

Joined: May 10, 2001
Posts: 309
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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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

or


"I'm not back." - Bill Harding, Twister
Peter Kryszkiewicz
Greenhorn

Joined: Oct 25, 2003
Posts: 6
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.
 
 
subject: relative URL in jar files