[英]java.util.MissingResourceException when running a jar
In order to internationalise my code, I use a property file called MessagesBundle_en_UK.properties which is located in a resource folder called config so that I the following structure: 为了使代码国际化,我使用了一个名为MessagesBundle_en_UK.properties的属性文件,该文件位于名为config的资源文件夹中,因此我具有以下结构:
+---src
\---com
\---proj
\---messages
Messages.java
+---config
\---languages
MessagesBundle_en_UK.properties
Messages.java has: Messages.java具有:
currentLocale = new Locale("en", "UK");
messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);
and I have added config/languages at the beginning of the classpath and it works fine when I run the program with javac/java from the command line. 并且我在类路径的开头添加了config /语言,当我从命令行使用javac / java运行程序时,它可以正常工作。
My problem is that it doesn't work when I run the jar and I get this MissingResourceException error. 我的问题是,当我运行jar并收到MissingResourceException错误时,它不起作用。
although the manifest does include config/languages: 尽管清单确实包含配置/语言:
Class-Path:
./config/languages
./libs/xxxx.jar
./libs/...
...
I'm bit confused... 我有点困惑...
Thanks for the help David 感谢大卫的帮助
您应该引用ResourceBundle相对于根的位置,就好像它是包的成员一样:
messages = ResourceBundle.getBundle("config.lagnuages.MessagesBundle",currentLocale);
I think the issue here is that you're trying to add a directory to the manifest Class-Path
and but don't end it with a /
character. 我认为这里的问题是,您正在尝试将目录添加到清单
Class-Path
,但不要以/
字符结尾。 From the Extension Mechanism documentation : 从扩展机制文档中 :
An application (or, more generally, JAR file) specifies the relative URLs of the optional packages (and libraries) that it needs via the manifest attribute
Class-Path
.应用程序(或更一般而言,JAR文件)通过清单属性
Class-Path
指定所需的可选包(和库)的相对URL。 This attribute lists the URLs to search for implementations of optional packages (or other libraries) if they cannot be found as optional packages installed on the host Java virtual machine*.如果找不到作为宿主Java虚拟机上安装的可选软件包的可选软件包(或其他库)的实现,则此属性列出了URL,以搜索这些实现。 These relative URLs may include JAR files and directories for any libraries or resources needed by the application or optional package.
这些相对URL可能包括应用程序或可选程序包所需的任何库或资源的JAR文件和目录。 Relative URLs not ending with '/' are assumed to refer to JAR files.
假定不以'/'结尾的相对URL引用JAR文件。 For example,
例如,
Class-Path: servlet.jar infobus.jar acme/beans.jar images/
Most people bundle their properties files in their jars. 大多数人将其属性文件捆绑在jar中。 For example:
例如:
+---jar_root
\---com
\---proj
\---messages
Messages.class
MessagesBundle_en_UK.properties
This bundle would have the name "com.proj.messages.MessageBundle"
. 该捆绑包的名称为
"com.proj.messages.MessageBundle"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.