java 找不到 ResourceBundle 文件,jar 中没有属性文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11667638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ResourceBundle file not found, properties file not in jar
提问by Jeankes
About the resourcebundle errors, I've read other questions about the same subject, but I didn't get any wiser. Tried different things, doesn't help. To give a little background: I developed a small app (JDK6), that does some flat file parsing and editing automatically. The app starts by reading some data from a properties file.
关于资源包错误,我已经阅读了关于同一主题的其他问题,但我没有更明智。尝试了不同的东西,没有帮助。提供一点背景知识:我开发了一个小应用程序 (JDK6),它自动执行一些平面文件解析和编辑。该应用程序首先从属性文件中读取一些数据。
Important: I want to be able to change the properties file. I don't want to put it in the jar file.
重要提示:我希望能够更改属性文件。我不想把它放在 jar 文件中。
However, even with the little example app that I tried creating, based on another questions/answer [here][1]:
但是,即使使用我尝试创建的小示例应用程序,基于另一个问题/答案 [here][1]:
package restestapp;
import java.util.Locale;
import java.util.ResourceBundle;
public class ResourceBundleTester
{
public static void main(String[] args)
{
Locale locale = Locale.getDefault();
String basename ="myresource";
ResourceBundle resourceBundle = ResourceBundle.getBundle(basename, locale);
System.out.println(resourceBundle.getString("STARTING_MYAPP"));
}
}
Both files are here:
两个文件都在这里:
/home/dakoina/Documents/ResTestapp/ResTestApp.jar
/home/dakoina/Documents/ResTestapp/myresource.properties
or even
甚至
c:/temp/ResTestApp.jar
c:/temp/myresource.properties
But when I run it, it gives me this output:
但是当我运行它时,它给了我这个输出:
dakoina@ubuntu:~/Documents/ResTestapp$ java -cp /home/dakoina/Documents/ResTestapp/ -jar "ResTestApp.jar"
Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name myresource, locale en_US
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1539)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1278)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:805)
at restestapp.ResTestApp.main(ResTestApp.java:19)
and in Windows, the same...
在 Windows 中,同样的......
c:\temp>java -jar ResTestApp.jar
Exception in thread "main" java.util.MissingResourceException: Can't find bundle
for base name myresource, locale en_US
at java.util.ResourceBundle.throwMissingResourceException(Unknown Source)
at java.util.ResourceBundle.getBundleImpl(Unknown Source)
at java.util.ResourceBundle.getBundle(Unknown Source)
at restestapp.ResTestApp.main(ResTestApp.java:19)
[1]: http://stackoverflow.com/questions/3742158/using-resourcebundle-with-an-external-file-java
I can't see where I am wrong :/
我看不出我错在哪里:/
回答by JB Nizet
-jar
means: compose the classpath from the given jar and from all the ones it references in its manifest. So the -cp
option is ignored.
-jar
意味着:从给定的 jar 和它在其清单中引用的所有 jar 组合类路径。因此该-cp
选项被忽略。
Your command line should be:
你的命令行应该是:
java -cp /home/dakoina/Documents/ResTestapp;/home/dakoina/Documents/ResTestapp/ResTestApp.jar the.main.Class
That said, a resource bundle is used to load internationalized labels. If you just want to read properties, you'd bette use the java.util.Properties
class.
也就是说,资源包用于加载国际化标签。如果您只想读取属性,则最好使用java.util.Properties
该类。
回答by Jigar Joshi
myresource.properties
needs to be in classpath for jar file
myresource.properties
需要在 jar 文件的类路径中
put this properties file into jar and configure classpath (as described in the link)
将此属性文件放入 jar 并配置类路径(如链接中所述)