Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Loading an Image from a Relative Path  RSS feed

 
Sam Sylva
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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!
 
Carey Brown
Bartender
Posts: 2985
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Open as a resource. Something like this.
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you discovered the Oracle tutorials? Loading Images Using getResource
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!