This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

relative URL in jar files  RSS feed

 
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?
 
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:
 
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.
 
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.
 
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!