roger_that
roger_that

Reputation: 9791

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.

Upvotes: 48

Views: 269424

Answers (14)

Pipo
Pipo

Reputation: 5073

In my particular case of injected AppContext and in a @PostConstruct context I had to add @LocalBean for the init() method to work and find the bundle

@Singleton(name = "AppContext")
@Startup
@LocalBean
public class AppContext {

    @PostConstruct
    public void init() {
        ResourceBundle resources = ResourceBundle.getBundle("propertiesFileNameWithoutExtension");
    }
    
}


@RequestScoped
class Foo {

    @Inject
    protected AppContext appContext;
    
    ...
}

Upvotes: 0

user12579252
user12579252

Reputation: 21

I had the same problem, but when I moved two (name. properties or name.config) files to "NetBeansProjects\projectName\target\classes", I mean locate your properties file or config file in target\classes directory in your project director, because once it's compiled one copy goes there so if you put early it's working. I am using Netbean Apache IDE 14. for you it should be in PBSkyScraper\build\skyscrapper.properties directory.

Upvotes: 0

DocJ457
DocJ457

Reputation: 877

if you are using maven try this:

public static Connection getConnection(){
    //*******************************************************************
    try(InputStream input = new FileInputStream("path to file")){
        Properties prop = new Properties();
        prop.load(input);
        String driver = prop.getProperty("driver");
        String url = prop.getProperty("url");
        String user = prop.getProperty("user");
        String password = prop.getProperty("password");
        //Class.forName(driver);
        connection = DriverManager.getConnection(url, user, password);
    } catch (Exception e) {
        e.printStackTrace();

    }

    return connection;

Upvotes: 0

Check project build path. May be only the *.java files are included. This problem occured in my Maven project. I needed to alter my pom.xml file.

<build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
</build>

Upvotes: 1

iamfnizami
iamfnizami

Reputation: 183

You can try anyone with resources-

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

By default it tries to find 'skyscrapper.properties' file inside 'src' but you have placed your file inside a sub-directory 'resources' which is unreachable.

In a maven project-

It looks like- 'src/main/resources/skyscrapper.properties' then use-

private static final String FILENAME = "skyscrapper";

And if it looks like- 'src/main/resources/prop/xyz/skyscrapper.properties' then use-

private static final String FILENAME = "prop/xyz/skyscrapper";
private static final String FILENAME = "prop.xyz.skyscrapper";

In this case by default it tries to find 'skyscrapper.properties' file inside 'src/main/resources' but your file is actually inside a subdirectory of resources then you need to provide relative path otherwise you may receive exception like-

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name src\main\resources\prop\xyz\skyscrapper.

Upvotes: 0

Sudheer Singh
Sudheer Singh

Reputation: 654

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);
    }
}

Upvotes: 3

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

Upvotes: 0

Etch
Etch

Reputation: 311

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!

Upvotes: 1

Alireza Alallah
Alireza Alallah

Reputation: 2534

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

Upvotes: 4

TontonZition
TontonZition

Reputation: 57

With Eclipse and Windows:

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

Upvotes: 1

peihan
peihan

Reputation: 652

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.

Upvotes: 2

Sudeep Krishnan M
Sudeep Krishnan M

Reputation: 141

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!!!

Upvotes: 6

Ross Drew
Ross Drew

Reputation: 8246

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);

Upvotes: 19

Bludzee
Bludzee

Reputation: 2899

Try with the fully qualified name for the resource:

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

Upvotes: 39

Related Questions