• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Loading an Image from a Relative Path

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This seems like it should be simple, but I cannot for the life of me figure it out. All I want to do is load an image into a swing JLabel and display it, but I'm going to have to make a JAR of the program and submit it for grading, so I need to be able to get the path of the image file relative to the JAR (at least I think that's how it works - I've never made a JAR before. Right now I'm just trying to load it from a path relative to my netbeans project). I've tried at least 20 different combinations of paths, file locations within the project, etc., but nothing works. I found this and tried to do what people were suggesting, but it didn't work and I don't understand it very well (I've never seen the class/ClassLoader thing before and after trying to read through the documentation for them I'm still pretty iffy on what they're all about).

Anyways, here's the test class I've been using to try to figure this out in its current iteration. Right now the image I want to load is in src/resources and the class is in src/imagedisplaytest


What's the best way to do this? Also, if anybody has any good resources for learning about the class and/or ClassLoader things could you please post a link to them? Thanks!
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Open as a resource. Something like this.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest rather than getting a path, use the URL constructor for ImageIcon.



Now its not completely obvious from your post, so forgive me if this was completely obvious to you and you are already doing this.
A JAR file is basically just a zip archive under a different name. You can open jar files in any zip tool (try it by renaming a .jar file to .zip)
So given that little fact, I would suggest you bundle the image you want inside the jar file rather than look for it relative to the jar.
 
Sam Sylva
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Huh, these work. I guess I got the relative path string right but the rest of the syntax wrong. Thanks! Also yeah, I phrased the part about the JAR file incorrectly. I'm assuming you can reference the relative location of a file inside a JAR just like I'm doing now with my NetBeans project, right?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you discovered the Oracle tutorials? Loading Images Using getResource
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Sylva wrote:Also yeah, I phrased the part about the JAR file incorrectly. I'm assuming you can reference the relative location of a file inside a JAR just like I'm doing now with my NetBeans project, right?



If your jar file is in the classpath, you can reference a file in the jar just like a file on the hard drive. Does that answer your question?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic