Search code examples
javapropertiesconfigurationconfiguration-filesresourcebundle

ResourceBundle - Cannot work out how a properties file is read when it does not exist in the filesystem


I have an EJB 2.1 application that refers to a properties file to read a value from. The filename of the properties file is defined as

private static final String CONFIG_DIRECTORY = "config.";
private static final String PROPERTIES_FILE = CONFIG_DIRECTORY + "myapp";

To load and read the property from the properties file, the following call is made passing in PROPERTIES_FILE as the name of the propertyFileName

this.getProperties(PROPERTIES_FILE);

The getProperties method uses ResourceBundle to load the properties file as shown below.

public Properties getProperties (String propertyFileName) {
        Properties properties = new Properties();
        ResourceBundle resourceBundle = ResourceBundle.getBundle(propertyFileName);
        Enumeration<String> keys = resourceBundle.getKeys();

        while (keys.hasMoreElements()) {
            String key = (String)keys.nextElement();
            String value = resourceBundle.getString(key);
            properties.put(key, value);
        }
        return properties;
    }

Now i have spent ages trying to find a file called config.myapp everywhere within the application's file system but it does not exist. I even searched the whole filesystem for that file but it is not there. I looked inside all the folders, jar files etc but i cannot find a file called config.myapp

There is a folder called 'config' in the root directory of the application and within that folder there is a file called myapp.properties which does have the property that is being read by the application. Could it be be that config.myapp is actually referring to the config/myapp.properties file? If so how does that work? Does ResourceBundle somehow translate config.myapp to config/myapp.properties?


Solution

  • Replacing the dots with slashes and appending .properties is the expected behavior. From the docs for ResourceBundle.getBundle:

    Otherwise, getBundle attempts to locate a property resource file. It generates a path name from the candidate bundle name by replacing all "." characters with "/" and appending the string ".properties". It attempts to find a "resource" with this name using ClassLoader.getResource.