Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 511
roger_that Avatar asked Nov 22 '13 10:11

roger_that


2 Answers

Try with the fully qualified name for the resource:

private static final String FILENAME = "resources/skyscrapper"; 
like image 190
Bludzee Avatar answered Sep 23 '22 19:09

Bludzee


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); 
like image 45
Ross Drew Avatar answered Sep 23 '22 19:09

Ross Drew