Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

properties - java.util.MissingResourceException: Can't find bundle for base name 'property_file name', locale en_US

I am trying to create a utility class ReadPropertyUtil.java for reading data from property file. While my class is located under a util directory , my skyscrapper.properties file is placed in some other directory.

But , when i try to access the properties using [ResourceBundle][1], i get exceptions, that bundle can't be loaded.

Below is the code on how I am reading the properties and also an image which shows my directory structure.

ReadPropertiesUtil.java

/**
 * Properties file name.
 */
private static final String FILENAME = "skyscrapper";

/**
 * Resource bundle.
 */
private static ResourceBundle resourceBundle = ResourceBundle.getBundle(FILENAME);

/**
 * Method to read the property value.
 * 
 * @param key
 * @return
 */
public static String getProperty(final String key) {
    String str = null;
    if (resourceBundle != null) {
        str = resourceBundle.getString(key);
            LOGGER.debug("Value found: " + str + " for key: " + key);
    } else {
            LOGGER.debug("Properties file was not loaded correctly!!");
    }
    return str;
}

Directory Structure

enter image description here

This line is giving the error private static ResourceBundle resourceBundle = ResourceBundle.getBundle(FILENAME);

I am unable to understand why isn't this working and what is the solution. The src folder is already added in build path completely.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try with the fully qualified name for the resource:

private static final String FILENAME = "resources/skyscrapper";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...