Change resource folder?

Java by default searches for files in a project, for example. I am creating a project called a calculator and I want a background image, I would put the necessary images in the project folder. I would like it to be more accurate by changing where Java searches for images.

I created a folder called res and added it to the build path, but if I had placed the image in this res folder, he would not have found it. For instance:

launcher.setIconImage(new ImageIcon("Logo.png").getImage()); 

This will not change IconImage if I put Logo.png in the res folder, and if I put it in the project folder. When I say the project folder, I mean that the eclipse folder is created when creating a new Java project.

+6
source share
2 answers

The project folder is simply the folder in which your program is running. Therefore, when you run your program from eclipse, it looks for the file "Logo.png" in the root directory of the project.

On the other hand, if you export your program and run it in another folder, this folder is located in the root folder of your program, and there it will search for PNG.

If you want your files to be in the res folder, you need to link to PNG accordingly, so, for example, you should write:

 launcher.setIconImage(new ImageIcon("res/Logo.png").getImage()); 

And if you want to make this more general, you should use some kind of configuration mechanism that provides you with the right path for your images. Therefore, you need to change one place when you want to change the folder name.

+1
source

Like @lwi, you must add the folder name to the resource path: "res / Logo.png" instead of "Logo.png".

If you want your images to be included in the JAR when exporting your program, I suggest you take a look at this question

Make sure you put your images in a subfolder of the source folder (as described in this ).

+2
source

All Articles