• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

relative URL in jar files

 
Peter Kryszkiewicz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Eugene Kononov:


SHould read

Correct me if I am wrong..
Shankar.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ClassLoader.getSystemResource()
is a convenience method, equivalent to
ClassLoader.getSystemClassLoader().getResource()
as far as I know.
 
shankar vembu
Ranch Hand
Posts: 309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peter Kryszkiewicz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic