Jason S
Jason S

Reputation: 189626

where to put .properties files in an Eclipse project?

I have a very simple properties file test I am trying to get working: (the following is TestProperties.java)

package com.example.test;

import java.util.ResourceBundle;

public class TestProperties {
    public static void main(String[] args) {
        ResourceBundle myResources =
              ResourceBundle.getBundle("TestProperties");
        for (String s : myResources.keySet())
        {
            System.out.println(s);
        }
    }

}

and TestProperties.properties in the same directory:

something=this is something
something.else=this is something else

which I have also saved as TestProperties_en_US.properties

When I run TestProperties.java from Eclipse, it can't find the properties file:

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

Am I doing something wrong?

Upvotes: 14

Views: 65505

Answers (5)

JozefP
JozefP

Reputation: 131

Do NOT put your propierties files into your src folder! Obviously that works, but basically this is NOT how you should approach your problems. Create a new folder in your project, for instance a 'Resources' folder, add it to the classpath in project properties and put all files other than .java there.

Upvotes: 13

sensoft1988
sensoft1988

Reputation: 11

put the TestProperties_en_US.properties(propery) file in the src folder and then run the program it will run

Upvotes: 1

Shannon Gerry
Shannon Gerry

Reputation: 21

I have just been trying to solve this problem as well, I have found that you must refresh Eclipse's list of files before you try to run your project. Then you can have your files in the base directory and use them as normal.

Upvotes: 2

Jason S
Jason S

Reputation: 189626

Aha, thanks a bunch. This also works.

package com.example.test;

import java.util.ResourceBundle;

public class TestProperties {
    public static void main(String[] args) {
        ResourceBundle myResources =
           ResourceBundle.getBundle(TestProperties.class.getCanonicalName());
        for (String s : myResources.keySet())
        {
            System.out.println(s);
        }
    }
}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499770

Put it at the root level of one of your source paths, or fully qualify the resource name in the call to getBundle, e.g.

ResourceBundle myResources =
          ResourceBundle.getBundle("com.example.test.TestProperties");

See the docs for ResourceBundle.getBundle(String, Locale, ClassLoader) for more information.

Upvotes: 31

Related Questions