[java] 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.

This question is related to java properties resourcebundle

The answer is


The simplest code would be like, keep your properties files into resources folder, either in src/main/resource or in src/test/resource. Then use below code to read properties files:

public class Utilities {
    static {
        rb1 = ResourceBundle.getBundle("fileNameWithoutExtension"); 
              // do not use .properties extension
    }
    public static String getConfigProperties(String keyString) {
        return rb1.getString(keyString);
    }
}

Use the Resource like

ResourceBundle rb = ResourceBundle.getBundle("com//sudeep//internationalization//MyApp",locale);
or
ResourceBundle rb = ResourceBundle.getBundle("com.sudeep.internationalization.MyApp",locale);

Just give the qualified path .. Its working for me!!!


just right click on the project file in eclipse and in build path select "Use as source folder"...It worked for me


You should set property file name without .properties extension, it works correctly for me:)


With Eclipse and Windows:

you have to copy 2 files - xxxPROJECTxxx.properties - log4j.properties here : C:\Eclipse\CONTENER\TOMCAT\apache-tomcat-7\lib


ResourceBundle doesn't load files? You need to get the files into a resource first. How about just loading into a FileInputStream then a PropertyResourceBundle

   FileInputStream fis = new FileInputStream("skyscrapper.properties");
   resourceBundle = new PropertyResourceBundle(fis);

Or if you need the locale specific code, something like this should work

File file = new File("skyscrapper.properties");
URL[] urls = {file.toURI().toURL()};
ClassLoader loader = new URLClassLoader(urls);
ResourceBundle rb = ResourceBundle.getBundle("skyscrapper", Locale.getDefault(), loader);

I'd like to share my experience of using Ant in building projects, *.properties files should be copied explicitly. This is because Ant will not compile *.properties files into the build working directory by default (javac just ignore *.properties). For example:

<target name="compile" depends="init">
    <javac destdir="${dst}" srcdir="${src}" debug="on" encoding="utf-8" includeantruntime="false">
        <include name="com/example/**" />
        <classpath refid="libs" />
    </javac>
    <copy todir="${dst}">
        <fileset dir="${src}" includes="**/*.properties" />
    </copy>
</target>

<target name="jars" depends="compile">
    <jar jarfile="${app_jar}" basedir="${dst}" includes="com/example/**/*.*" />
</target>

Please notice that 'copy' section under the 'compile' target, it will replicate *.properties files into the build working directory. Without the 'copy' section the jar file will not contain the properties files, then you may encounter the java.util.MissingResourceException.


I have just realized that my error was caused in the naming convention of my property file. When i used xxxx.xxxx.properties i got the error:

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

Changing it to something like xxx-xxxx.properties works like a charm. Hope i help someone!


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to properties

Property 'value' does not exist on type 'EventTarget' How to read data from java properties file using Spring Boot Kotlin - Property initialization using "by lazy" vs. "lateinit" react-router - pass props to handler component Specifying trust store information in spring boot application.properties Can I update a component's props in React.js? Property getters and setters Error in Swift class: Property not initialized at super.init call java.util.MissingResourceException: Can't find bundle for base name 'property_file name', locale en_US How to use BeanUtils.copyProperties?

Examples related to resourcebundle

java.util.MissingResourceException: Can't find bundle for base name 'property_file name', locale en_US Format a message using MessageFormat.format() in Java Resource files not found from JUnit test cases How to use UTF-8 in resource properties with ResourceBundle How to load a resource bundle from a file resource in Java?