What is the best way to specify the file path in a Java program? Where is the best place to store the files used by the program?


You can specify the absolute path to the file, let's say like this:

Image img = new ImageIcon("G:\\img.png").getImage();

As far as I understand, it is quite bad to do this, because we do not know where the user will save our program.

You can specify a relative path to the file, say like this:

Image img = new ImageIcon("img.png").getImage();

But then (if only I understood correctly from the experiments done) to get the full path to the file, the JVM does a "concatenation" of the current directory with the prescribed relative path. I.e. if I I run the program with this command:

G:\program\bin>java -jar Program.jar

Then the JVM will search for the file at this location:

G:\program\bin\img.png

As far as I understand, it is quite bad to do this, because we do not know from which directory the user will run our program.

You can do something interesting and difficult, like this:

Image img = new ImageIcon( this.getClass().getResource("img.png") ).getImage();

Here, the file search will occur from the package where the class this.getClass() is located. (i.e., the same "concatenation" occurs with the path to the class package)

But surely probably bad to mix class files with resource files?

Or even so:

Image img = new ImageIcon( this.getClass().getResource("/img.png") ).getImage();

Here, the file is searched in the classpath directories. But in theory, then there may be several suitable files? And what will it be?!

In general, questions: Is what I wrote correct? What other ways are there? When which method to use? What is the best way (if any)?

Author: zer_ik, 2016-02-24

2 answers

The general answer to this is simply the word "depends" - the main question is what you are working with. As a rule, java applications are distributed as jar archives, and if you have resources distributed with the application (for example, icons), then they should either be there, among the classes, or if you distribute the application as an archive and separate resources outside the archive, then you need to find the location of the jar file yourself and manually restore the path to the resource by default analogies to the working directory. If you are writing some utility that processes arguments, then you will most likely have to deal with both absolute and relative paths - if you are writing, say, an image resizer, then calling java -jar path/resizer.jar image.png 300x400 will require simply passing the argument inside ImageIcon directly, without any analysis at all.

In other words, all the described methods have their own application, and are more or less preferred depending on the situation; each of them is not something bad, and is not applicable in the current situation. If the question is to search for application resources, then it is best to either throw it into the jar, or look for the application installation location and count from it (since working dir can turn out to be anything).

 9
Author: etki, 2016-02-24 18:10:55

No one prevents you from using your images and other files in the same directory without an absolute path:

String path = new File("").getAbsolutePath();

Just store the images, let's say in the img folder, yourself .the jar file will be in the root folder. As a result, you drag your images along the path path + "\img".

Similar theme (only with file creation).

 1
Author: Denis, 2017-04-13 12:53:25