-1

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.

Thanasis1101
  • 1,614
  • 4
  • 17
  • 28

1 Answers1

2

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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you very much for the quick and accurate response ! One more question though. In getResources the {path to images} where does it begin? For example if I have the images in the src folder, do I write "src/images/image.jpg" ? And what if it is outside the src in a folder named images? – Thanasis1101 Jan 15 '17 at 22:11
  • 1
    No, treat the path as if `src` is the root`, I should be more like `/images/image.jpg`. This is then (by the system) appended to each path in the classpath and checked to see if it returns a valid result. The `src` path won't exist once the app is packaged – MadProgrammer Jan 15 '17 at 22:14
  • Why would this attract a downvote? In what ways does it not answer the OP's question? Some idea of why you think it's an inappropriate question would go a long way to helping me improve it (and other questions in the future) – MadProgrammer Jan 16 '17 at 22:24