Java project with image files

2020-05-03 09:25发布

I am working on a project in which I use image files. Where is the proper location to store the folder with the images? Inside src folder (created by netbeans) or outside? Also when I clean and build the project a dist folder is created with the jar file in it. Can the images folder be included in the jar or I have to copy them in the same folder with the jar if I want to run the program on another computer? Finally, I would like to know how to use the getResource when I want to refer to the image folder from the source code. Thanks in advance.

1条回答
姐就是有狂的资本
2楼-- · 2020-05-03 10:08

Where is the proper location to store the folder with the images? Inside src folder (created by netbeans) or outside?

The answer is, it depends. If you want the images bundled within the Jar (AKA embedded resource), then they should be stored within the Netbeans src directory. If you want them stored externally (and you will then become responsible for ensuring that they are included with the Jar when you deploy it), then they should be stored outside the src directory.

Can the images folder be included in the jar or I have to copy them in the same folder with the jar if I want to run the program on another computer?

If the images are stored within the src directory, they are included automatically, if they stored externally, you will need to provide some custom ant code to perform the required actions you want.

Again, if they are stored externally (to the src directory), you become fully responsible for their management

Finally, I would like to know how to use the getResource when I want to refer to the image folder from the source code.

BufferedImage img = ImageIO.read(getClass().getResource("{path to images}/{image name}");

There are a number of resources/examples available which can expand on the individual requirements/differences in how to use getResource in different scenarios, but that's the basic idea

查看更多
登录 后发表回答