wandermonk
wandermonk

Reputation: 7356

ResourceBundle throws MissingResourceException

enter image description here

I am trying to read properties file from the file location. But, the resource bundle is unable to find the file and throws the below exception

I tried keeping the file under the classpath but did not succeed reading the file. But, my intention is to read the property file from a file path and this comes from user. So, that my class can read agnostic file inputs to get the properties.

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name configFileName, locale en_US
    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1499)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1322)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:721)
    at com.cisco.propertiesreader.FileConfigLoader.getBundle(FileConfigLoader.java:16)
    at com.cisco.propertiesreader.MainApp.main(MainApp.java:10)

FileConfigLoader class:

package com.cesna.propertiesreader;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

public class FileConfigLoader {

    private static final Logger LOGGER = Logger.getLogger(FileConfigLoader.class);

    private static String EMPTY = "";

    public static ResourceBundle getBundle(String configLocation) {

        ResourceBundle rb = ResourceBundle.getBundle("configFileName");

        return rb;

    }

    public static String getValue(ResourceBundle rb, String key) {
        String value = EMPTY;

        try {
            value = rb.getString(key);
        } catch (MissingResourceException mREx) {
            LOGGER.error("Missing Resource : " + key);
        }

        return value;
    }

}

MainApp Class:

package com.cesna.propertiesreader;

import java.util.ResourceBundle;

public class MainApp {

    public static void main(String[] args) {

        ResourceBundle resource = FileConfigLoader
                .getBundle("D:\\PDIWorkspace\\PropertyFileReader\\resoures\\config.properties");

        String hive_db = FileConfigLoader.getValue(resource,"hive_db");

        System.out.println(hive_db);
    }

}

Am, I doing it the right way.

config.properties

hive_db=installbase
EDGE_HIVE_CONN=dev-node
target_dir=/app/dev/SmartAnalytics/sqoop_temp/
IB_log_table=IB_log
SR_DG_master_table=data_usage_governance_master
SR_DG_table=data_usage_governance_log

Issue Resolved after using the below way to read file:

package com.cesna.propertiesreader;

import java.util.ResourceBundle;

public class MainApp {

    public static void main(String[] args) {

        ResourceBundle resource = FileConfigLoader
                .getBundle("com.cisco.propertiesreader.config");

        String hive_db = FileConfigLoader.getValue(resource,"hive_db");

        System.out.println(hive_db);
    }

}

Upvotes: 0

Views: 3108

Answers (2)

pnkjrn9
pnkjrn9

Reputation: 1

public static ResourceBundle getBundle(String configLocation) {
   ResourceBundle rb = ResourceBundle.getBundle("./resources/"+configLocation);
   return rb;
}

Upvotes: 0

mh-dev
mh-dev

Reputation: 5503

Are you sure that the following is intended

public static ResourceBundle getBundle(String configLocation) {

    ResourceBundle rb = ResourceBundle.getBundle("configFileName");

    return rb;

}

I think it should be

public static ResourceBundle getBundle(String configLocation) {

    ResourceBundle rb = ResourceBundle.getBundle(configLocation);

    return rb;

}

If you want to load the data from an external directory (not in classpath)

public static ResourceBundle getBundle(String basePath, String baseName) throws MalformedURLException {
    File file = new File(basePath);
    URL[] urls = {file.toURI().toURL()};
    ClassLoader loader = new URLClassLoader(urls);
    ResourceBundle rb = ResourceBundle.getBundle(baseName, Locale.getDefault(), loader);
    return rb;
}   

Upvotes: 1

Related Questions