views:

1712

answers:

2

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?

+3  A: 

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.

Jon Skeet
DOHWWW! Thanks. I wish when tutorials mentioned the use of basic stuff like this, they were more explicit.
Jason S
A: 

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