java.util.MissingResourceException

前端 未结 6 1121
你的背包
你的背包 2021-01-05 15:56

I am getting below exception while running an application. This application read abc.properties file,

Exception in thread \"main\" java.util.MissingResourceExcept         


        
相关标签:
6条回答
  • 2021-01-05 16:20

    You just need to add the package name while getting the file

    For example if your properties name is "ABC.properties" in package a.b.c then the below code will work perfectly

    ResourceBundle labels = ResourceBundle.getBundle("a.b.c.ABC");
    
    0 讨论(0)
  • 2021-01-05 16:22

    Follow the hints in this post and see if you made one of those mistakes, which could be (copy pasted from the link):

    1. These resource properties files are loaded by classloader, similar to java classes. So you need to include them in your runtime classpath.

    2. These resources have fully-qualified-resource-name, similar to a fully-qualified-class-name, excerpt you can't import a resource into your java source file. Why? because its name takes the form of a string.

    3. ResourceBundle.getBundle("config") tells the classloader to load a resource named "config" with default package (that is, no package). It does NOT mean a resource in the current package that has the referencing class.

    4. ResourceBundle.getBundle("com.cheng.scrap.config") tells the classloader to load a resource named "config" with package "com.cheng.scrap." Its fully-qualified-resource-name is "com.cheng.scrap.config"

    0 讨论(0)
  • 2021-01-05 16:29

    I had the same issue today and it took me a while until I figured out a fix (I'm using Eclipse as an IDE). My project folder contains the *.properties-Files in the following path:

    project/src/strings
    

    Since strings is a sub-folder of src and src is already in the build path of the project (see Property Window), all I needed was to add an Inclusion-Pattern for the *.properties-Files:

    **/*.properties
    

    There should be already an Inclusion-Pattern for *.java-Files (**/*.java)

    Maybe there is something similar for your IDE

    0 讨论(0)
  • 2021-01-05 16:35

    Just copy the resource file over to where your class files are.

    In my case my directory was:

    bin 
      - com 
         - brookf 
            - all my packages here. 
    

    copy your resource file to the bin folder.

    0 讨论(0)
  • Loading the Properties file for localization stuff is also affected by the package naming. If you put your Properties file in a package like org.example.com.foobar and load it just by its name abc you have to add the prefix org.example.com.foobar, too. If you have the properties at a different location (like in the root directory or in another subdir) you have to change either the name to load or the classpath.

    I run fine in placing the properties file in the same location where the .java file is and using something like

    private static ResourceBundle RES = ResourceBundle.getBundle(NameOfTheCurrentClass.class.getCanonicalName());
    
    0 讨论(0)
  • 2021-01-05 16:45

    Is the file in your classpath? If it is, try renaming it to abc_en_US.properties

    0 讨论(0)
提交回复
热议问题