This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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
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
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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!