I thought something like this would be enough. I even put the image in the same folder as the Eclipse project. However...I think I needed the absolute path. Also, it's just occurred to me that I have no clue how it'd find that Image file if I turned the program into a jar file and sent it to another computer. Any ideas?
What you want to use is the method getResource() in the Class class. That looks for resources relative to where the class was loaded from. So, it will work just the same if your application is a bunch of .class files on the file system or packaged up in a Jar. For example:
will load an image that is in the same folder (in a Jar or otherwise) as the .class file of the current class. You can start the filename with "/" if you want a path relative to the the root of the Jar.
Note that ImageIcon has a constructor that takes a URL object as argument, which is what getResource() returns.
Also, is there a way to find a file or something if it's not right there? I mean...Windows search can find stuff hiding in the crevices of my computer hard drive.
Paul Adcock wrote: I thought a URL was a for webpages?
Also, how would you ship an image with a jar?
URLs are for locating a number of things. Web pages, files...and files within jars.
A jar is just a zip file, so sticking other resources in it isn't a problem. With most IDEs, if you put the image in same directory as your source (.java) file it'll automatically get packaged into the jar along with the .class file.
Paul Adcock wrote: I thought a URL was a for webpages?
Also, how would you ship an image with a jar?
URLs are for locating a number of things. Web pages, files...and files within jars.
A jar is just a zip file, so sticking other resources in it isn't a problem. With most IDEs, if you put the image in same directory as your source (.java) file it'll automatically get packaged into the jar along with the .class file.
I got it to work without the getResources thing. However, with it...it says I have a Null Pointer Exception. I saved the image inside the package that the class is part of. I tried saving to the .class file location but it said I couldn't save there. Where do I save it so it won't return null?
I need to know how to get the image to come with the class if I put it in a jar file and how exactly to get the getClass.getResources() to find it. So far I've been unsuccessful.
This is the sample program which displays an Image on a JLabel. This program Login.java is saved ina folder in E:\Inventory. So the full path of the program is E:\Inventory\Login.java. The image L3.jpg is in the location E:\Inventory\images. That is, the image L3.jpg is in th folder images. Now I hope you know what to do. Execute the code and enjoy!!
Using resources as in Matthew Brown's post is a lot better. That "." in your path is the current directory, where the application was started from. That may not be the root of the application, or the folder of the JAR file the application is in. Your code would then be broken.