为什么我得到 MissingResourceException

Why do I get MissingResourceException

为什么会抛出 MissingResourceException 异常?我是不是弄错了 ListResourceBundles 的重点是在代码中提供资源?

public class Hello extends Thread {

    public static void main(String[] args) {

         ResourceBundle resBundle = ResourceBundle.getBundle("ResourceBundle", new Locale("it", "IT", ""));
         System.out.println(resBundle.getObject(new Integer(1).toString()));
    }


}

在单独的文件中:

public class ResourceBundle_it_IT extends ListResourceBundle {

    public Object[][] getContents() {
        return contents;
    }
    static final Object[][] contents = {
        {"1", "Uno"},
        {"2", "Duo"},
        {"3", "Trie"},};

}

我的错误:

 run:
Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name ResourceBundle, locale it_IT
    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1564)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1387)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:845)
    at hello.Hello.main(Hello.java:21)
/home/maxbisesi/.cache/netbeans/8.1/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

当您遇到错误并试图了解原因时,第一步是始终读取错误的堆栈跟踪。在这种情况下,堆栈跟踪是

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name ResourceBundle, locale it_IT
    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1564)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1387)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:845)
    at Hello.main(Hello.java:9)
Caused by: java.lang.IllegalAccessException: Class java.util.ResourceBundle$Control can not access a member of class ResourceBundle_it_IT with modifiers ""
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)
    at java.lang.Class.newInstance(Class.java:436)
    at java.util.ResourceBundle$Control.newBundle(ResourceBundle.java:2645)
    at java.util.ResourceBundle.loadBundle(ResourceBundle.java:1501)
    at java.util.ResourceBundle.findBundle(ResourceBundle.java:1465)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1361)
    ... 2 more

如果你阅读它,问题就会变得很明显:

Class java.util.ResourceBundle$Control can not access a member of class ResourceBundle_it_IT with modifiers ""

将 class ResourceBundle_it_IT 移动到它所属的 onwn 文件中。按照错误信息的提示设置为public,问题就消失了。

通常发生在代表您的资源的 class 不是 public 时,因为 属性 值需要 public 才能被扫描,请检查您的 class 正确标记为 public