在 jpackaged Java 应用程序中部署可写属性文件

发布于 2025-01-15 00:31:47 字数 210 浏览 0 评论 0原文

我有一个 Java 客户端应用程序,我将使用 jpackage 将其打包为独立应用程序。

但是应用程序有一个属性文件,应用程序必须读取和写入该文件。我最初尝试在 IDE 中执行此操作,但是当我打包应用程序时,它失败了,因为属性文件的位置实际上位于打包的 .jar 内部。

是否有规范/“正确”的方法来使用 jpackaged 应用程序维护可写属性文件?

I have a Java client application that I am going to package as a stand-alone application using jpackage.

But the application has a Properties file which must be both read and written to by the application. My initial attempts to do this run in the IDE, but when I package the application it fails because the location of the properties file is actually located inside the packaged .jar.

Is there a canonical / "right" way to maintain a writable properties file with a jpackaged application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千鲤 2025-01-22 00:31:47

将默认设置打包到您的应用程序中。在运行时,将它们的可写副本存储在已知文件位置,通常在用户主目录的子目录中:

String applicationName = /* ... */;

Properties configuration;

String os = System.getProperty("os.name");
String home = System.getProperty("user.home");

Path localSettingsDir;
if (os.contains("Mac")) {
    localSettingsDir = Path.of(home, "Library", "Application Support");
} else if (os.contains("Windows")) {
    String appData = System.getenv("APPDATA");
    if (appData != null) {
        localSettingsDir = Path.of(appData);
    } else {
        localSettingsDir = Path.of(home, "AppData", "Roaming");
    }
} else {
    String configHome = System.getenv("XDG_CONFIG_HOME");
    if (configHome != null) {
        localSettingsDir = Path.of(configHome);
        if (!localSettingsDir.isAbsolute()) {
            localSettingsDir = Path.of(home, ".config");
        }
    } else {
        localSettingsDir = Path.of(home, ".config");
    }
}

Path localSettings = localSettingsDir.resolve(
    Path.of(applicationName, "config.properties"));

configuration = new Properties();
if (!Files.exists(localSettings)) {
    Files.createDirectories(localSettings.getParent());
    try (InputStream defaultSettings =
        MyApplication.class.getResourceAsStream("config.properties")) {

        Files.copy(defaultSettings, localSettings);
    }
}

try (Reader settingsSource = Files.newBufferedReader(localSettings)) {
    configuration.load(settingsSource);
}

有关确定系统应用程序配置目录的更多详细信息,请参阅 查找专用应用程序文件夹的位置

Package default settings in your application. At runtime, store a writable copy of them in a known file location, typically in a directory that is a descendant of the user’s home directory:

String applicationName = /* ... */;

Properties configuration;

String os = System.getProperty("os.name");
String home = System.getProperty("user.home");

Path localSettingsDir;
if (os.contains("Mac")) {
    localSettingsDir = Path.of(home, "Library", "Application Support");
} else if (os.contains("Windows")) {
    String appData = System.getenv("APPDATA");
    if (appData != null) {
        localSettingsDir = Path.of(appData);
    } else {
        localSettingsDir = Path.of(home, "AppData", "Roaming");
    }
} else {
    String configHome = System.getenv("XDG_CONFIG_HOME");
    if (configHome != null) {
        localSettingsDir = Path.of(configHome);
        if (!localSettingsDir.isAbsolute()) {
            localSettingsDir = Path.of(home, ".config");
        }
    } else {
        localSettingsDir = Path.of(home, ".config");
    }
}

Path localSettings = localSettingsDir.resolve(
    Path.of(applicationName, "config.properties"));

configuration = new Properties();
if (!Files.exists(localSettings)) {
    Files.createDirectories(localSettings.getParent());
    try (InputStream defaultSettings =
        MyApplication.class.getResourceAsStream("config.properties")) {

        Files.copy(defaultSettings, localSettings);
    }
}

try (Reader settingsSource = Files.newBufferedReader(localSettings)) {
    configuration.load(settingsSource);
}

For more details on determining a system’s application configuration directory, see Find place for dedicated application folder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文