Where should i place my files in order to be able to access them when i run the jar?

The answer will depend on if you want to write to the files or not…

You could…

Ensure that the files are placed relative to the Jar file and use a relative path. The problem with this is if the execution context is not the same directory as where the jar and files are stored, you won’t be able to find them again…

This will also be dependent on your build process to make sure that any required files are placed copied to the build location of the Jar

You could…

Place the files within a well know location, for example, on Windows you could use {user.home}/AppData/Local/{application name} or on Mac you could use {user.home}/Library/Application Support/{application name} and place the files here, then you could use an absolute path to the files

This likely becomes an installation issue, as you need to ensure that any required files are copied to the required location when the application is installed.

If the files are auto generated at runtime, then you just need to make sure the directories exists and make them if they don’t

You could…

Store the files within the Jar context (AKA embedded resources), the means by which you do this will depend on your IDE and build process, for example, in Netbeans, you can copy files into the src directory of your project and they will automatically be included in the resulting Jar file. I believe that Eclipse can work the same way. However, if you’re using Maven, you will need to place the files into the resources directory instead.

You would then access these resources using Class#getResource or Class#getResourceAsStream depending on your needs.

This will make the resources read-only however.

Leave a Comment