Forums Register Login

Loading an Image from a Relative Path

+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send

Open as a resource. Something like this.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
1
+Pie Number of slices to send: Send
Have you discovered the Oracle tutorials? Loading Images Using getResource
+Pie Number of slices to send: Send
 

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?
The knights of nee want a shrubbery. And a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 13466 times.
Similar Threads
How to update an image using JLabel without creating a new window.
Getting a Resource from a JAR file
Problems with File's listFiles()
How to display 92 jpg images from a folder one at a time in random order.
JScrollPane
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 05:46:48.