Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get location from where the Java code is executed

I have a swing Java application that preserves a lot of data (you can think of a game and its saves for example). Those data are stored in files rather than in database.

I would like to keep this files near installation files(.jar file) of my application.Some users(like me) are used to delete default application folder of OS when it gets large and I don't want them to loose their data this way.

Any ideas how to do this easily ? How do I get the folder of the .jar file from program that is executing form that .jar file? Or how do I output files directly into some package? How do I create packages(folders inside jar) dynamically ? Or is there a simple way to distribute Java application in other formats then .jar and then store generated data in installation (sub)folder ?

Thanks for reading

like image 822
Rasto Avatar asked Dec 09 '22 16:12

Rasto


2 Answers

I would like to keep this files near installation files(.jar file) of my application.Some users(like me) are used to delete default application folder of OS when it comes to large and I don't want them to loose their data this way.

This (and the concept of multi-user systems with no write access to application directories for regular user accounts) is the reason why user data should be kept in the user's home directory, and not anywhere near the app installation folder.

like image 189
Michael Borgwardt Avatar answered Dec 28 '22 06:12

Michael Borgwardt


The code

URL folderURL = ClassLoader.getSystemResource(".");

will provide a URL to the first location searched for resources like the class file you're currently executing.

like image 24
Stephen Denne Avatar answered Dec 28 '22 08:12

Stephen Denne