gpt4 book ai didi

java - 如何从类路径中获取 ResourceBundle 文件?

转载 作者:行者123 更新时间:2023-11-30 04:25:51 29 4
gpt4 key购买 nike

我正在使用不同语言(en、de、fr ...)的 ResourceBundle,并且 key 列在 .properties 文件中。它工作得很好,但只使用函数 getBundle() 中的类路径。

现在,如果我将项目构建为可执行 JAR 文件,我将无法维护这些属性/字典文件。我怎样才能继续从 JAR 文件中获取这些文件并使用它们(可能使用相对路径)?

最佳答案

检查this如果有帮助的话。该线程列出了一种使用绝对路径(而非相对)实现此目的的方法。

内联链接中的代码:

File file = new File("/languages");
URL[] urls = {file.toURI().toURL()};
ClassLoader loader = new URLClassLoader(urls);
ResourceBundle bundle = ResourceBundle.getBundle("english", Locale.getDefault(), loader);

也来自 URL:

    URL[] urls = new URL[1];

// - startfolder on the server, where he can find the package-structure like this.BUNDLE_NAME (http://somedomain.com/jws-stuff/com/myexample/test/)
urls[0] = new URL("http://somedomain.com/jws-stuff/");
ClassLoader loader = new URLClassLoader(urls);
RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME, Locale.GERMAN, loader);


关于java - 如何从类路径中获取 ResourceBundle 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15878227/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com