Problems with JPackage due to different path structure between Windows and Linux

Viewed 261

On Windows I have just converted my application installer from Izpack to JPackage and because of the structure created by JPackage I had to make some code changes to allow my application to find various config files, basically they are copied from app folder to C:\Users\Username\Appdata.... on first start (but bit more complicated than that).

I am now using JPackage on Linux, and the application copies config files from app to $HOME/.appname. However whereas on Windows app is a subfolder relative to launcher with Linux its now in ../lib/app so the code doesnt work

So I have to make some linux specific changes, but it got me wondering am I doing something very wrong here in order for me to have to make these changes ?

1 Answers

One way to load configuration files is using the classloader: the files are packaged in a jar along with the rest of your application and you load them as a stream, not as a file:

    Properties properties = new Properties();
    InputStream stream = YourApplication.class.getResourceAsStream("/resources/conf.properties");
    properties.load(stream);

The path (example) /resources/conf.properties is relative the .jar file where the file is bundled, so it does no depend on the installation folder.

The above example assumes that you're using a Properties file but this works for any resource you can load as a stream.

Related