关于ResourceBundle的找不到资源的问题

 

 

1)        目录结构:

|   build.xml
+---build
|   /---classes
|       /---com
|           /---nana
|               /---test
|                       Scrap.class
|
+---src
|   /---com
|       /--- nana
|           /--- test
|                   autogen.properties
|                   Scrap.java

 

2)        问题描述:

ResourceBundle rb = ResourceBundle.getBundle("autogen.properties",Locale.getDefault());

 

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name autogen.properties, locale zh_CN

    at java.util.ResourceBundle.throwMissingResourceException(Unknown Source)

    at java.util.ResourceBundle.getBundleImpl(Unknown Source)

    at java.util.ResourceBundle.getBundle(Unknown Source)

    at com.nana.test.AutoGen.main(AutoGen.java:25)

 

3)        问题解决:

1.这类resource properties文件是由classloader装载的,和java文件很类似,所以应该把这类文件加到classpath中。

2. ResourceBundle.getBundle("com.nana.test.autogen")告诉classloader装载一个资源,其名字是 "autogen",包名为 "com.nana.test" 

3. 如果想不写包名,直接获取autogen.properties,则需要把autogen.properties文件拷贝到classes/com/nana/test/目录下,和调用它的类文件一起。

ResourceBundle rb = ResourceBundle.getBundle("autogen",Locale.getDefault());

你可能感兴趣的:(java,thread,exception,ClassLoader,properties)