2

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 ?

4

1 回答 1

1

加载配置文件的一种方法是使用类加载器:文件与应用程序的其余部分一起打包在一个 jar 中,并将它们作为流加载,而不是作为文件加载:

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

路径(示例)/resources/conf.properties是相对于捆绑文件的 .jar 文件,因此它不依赖于安装文件夹。

上面的示例假设您使用的是属性文件,但这适用于您可以作为流加载的任何资源。

于 2020-08-17T19:20:31.157 回答