Leo Orientis Leo Orientis - 2 months ago 11x
Scala Question

Simple Java Desktop App: Config files and where to put them

This feels like an absolute newbie question, but I'm on a colossal yak-shave over what should be an elementary concern.

I'm writing a simple desktop app in Java. (Scala actually, but let that pass.) It targets Windows machines. I want to package it and have it runnable without creating an installer, or messing about with the Windows registry. And I want a config file that users can find and edit in order to customize settings. (In the manner of SublimeText's user preferences, for example.)

I envision the following structure:

|- bin/
|- myApp.class (or myApp.jar, if you prefer)
|- files/
|- config/
|- config.json
|- output/
|- output.xlsx

Inside Eclipse, relative paths tend to resolve to the
directory, but if I run the app from the command-line, they resolve to whatever the -cp argument was. (Generally, the

Am I being naïve? Have I got the wrong paradigm entirely?

It seems to me it would be a fundamental thing to be able to consistently locate files (resources) in the file system surrounding your executables.

Please help!


The class File resolves relative paths by default in the user's current working directory (available as system property user.dir). So, if you do cd on command line to change to a directory and then invoke the Java application this directory will be the current user's working directory from the point of view of the Java application.

One thing you can do is to pass your base-directory as system property to the Java application. This can be done within a shell script that starts your application by using it's own location.

Another possibility is to include the files like configuration file in classpath when invoking the Java application and to load them from classpath in Java application. The disadvantage of this solution is that the user can do more things than needed with damage as a possible result.