Java in Eclipse: Where do I put files on the filesystem that I want to load using getResource? (e.g. images for an ImageIcon)
For Eclipse, typically all you need to do is set up a folder somewhere within your source code directory. For instance, if the directory containing your source is /src
then you can create a /src/resources
folder to place your images/files in. Then, within your class you do a getResource("/resources/image.png")
to retrieve it.
You can also place the image/file within the same folder/package as the class trying to access it if you wish (example: place the image.png in the com.mycompany
package with the com.mycompany.Foo
class that needs to access it and call getResource("image.png")
), but I've found it's easier to keep resources like images and other files in their own special directory outside of the class folders -- they're just easier to manage that way.
In Eclipse, whenever you do a build, the files within this resource directory will be copied over into your build directory along with your compiled classes.
It's important to note that if you have "Build Automatically" turned on in Eclipse (as most people do) any resources in this directory that get changed outside of Eclipse (i.e. you edit an image using an image editing tool) that the IDE may not always detect this change. Usually doing a refresh on the project folder will ensure that the file gets updated in the build in these situations.
Adding images to eclipse java project using ImageIcon(getClass().getResource()
Make sure image file is present at correct location. It should be under src/images
folder.
You can try any one based on image location.
// Read from same package
ImageIO.read(getClass().getResourceAsStream("folder63.png"));
// Read from images folder parallel to src in your project
ImageIO.read(new File("images/folder63.jpg"));
// Read from src/images folder
ImageIO.read(getClass().getResource("/images/folder63.png"))
// Read from src/images folder
ImageIO.read(getClass().getResourceAsStream("/images/folder63.png"))
Read more...
It's worth reading Java Tutorial on Loading Images Using getResource
Unable to get resources using getResource()
Try something like this for setting the image icon
ImageIcon imageIcon = new ImageIcon(getClass().getResource("myImage.png"));
Your image file should be located within the src folder if your using an IDE. If your not using an IDE , put the image in the same folder as your .java files.
Images not loaded when exporting to runnable jar
In a jar, you need to access files as a resource by using code like this (similar for .txt .WAV):
ImageIcon ic = new ImageIcon(getClass().getResource("/machetelife.jpg"));
I don't know if eclipse packages it differently.. I usually use the cmd to package jar files.
Accessing image paths in Eclipse
You should have a resource folder with the folder named Images
in that, then it should work.
Example:
How I access those icons:
public BufferedImage icon32 = loadBufferedImage("/icon/icon32.png");
public BufferedImage icon64 = loadBufferedImage("/icon/icon64.png");
private BufferedImage loadBufferedImage(String string)
{
try
{
BufferedImage bi = ImageIO.read(this.getClass().getResource(string));
return bi;
} catch (IOException e)
{
e.printStackTrace();
}
return null;
}
How to import a file from resources folder in Eclipse for Java
The string which you pass as an argument to getResourceAsStream
has to be either the location relative to the class you call getResourceAsStream
from, or absolute w.r.t. any of your source folders, with a leading /
.
So if resources
is a source folder in your project (marked with a little package-like symbol in Eclipse) then getClass().getResourceAsStream("/BlankPDF.pdf")
should work.
Example: Say you have the following folders and packages in your project, source folders being marked with a *
:
Project
src*
com.package.foo
MyClass.java
resources*
BlankPDF.pdf
Then both MyClass.class.getResourceAsStream("/BlankPDF.pdf")
(leading /
, absolute) and MyClass.class.getResourceAsStream("../../../BlankPDF.pdf")
(no leading /
, relative) should get you the file.
Related Topics
The Difference Between the Runnable and Callable Interfaces in Java
H2 In-Memory Database. Table Not Found
Setting Jpg Compression Level with Imageio in Java
Java Keylistener Not Registering Arrow Keys
How to Read .Pem File to Get Private and Public Key
How Is an Instance Initializer Different from a Constructor
Why Equals() Method When We Have == Operator
How to Create an Array of Arraylists
How to Obtain Localdatetime from Temporalaccessor When Parsing Localdatetime (Java 8)
How to Handle Pop-Up in Selenium Webdriver Using Java
What's the Difference Between Getpath(), Getabsolutepath(), and Getcanonicalpath() in Java
How to Have 2 Jvms Talk to One Another
Try with Resources VS Try-Catch
How Cancel the Execution of a Swingworker
Multi-Project Test Dependencies with Gradle
Is an Array a Primitive Type or an Object (Or Something Else Entirely)
How to Get Out of While Loop in Java with Scanner Method "Hasnext" as Condition