Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.MissingResourceException: Can't find bundle for base name

I'm testing Java's i18n features and have a problem, I can't load the language file when it's not in the class root. Right now my files are in the /lang directory.

Looked several answers here in SO, putting it in a classes subdir and loading it like lang.Messages, used complete location routing /Test/lang/Message (test is the project name), using just /lang/Message and still I'm getting the:

java.util.MissingResourceException: Can't find bundle for base name

error.

Anything else to try?

My file structure is:

Test/lang/Messages_es.properties

Test/src/test/Main.java

import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.JFrame;

public class Main {

     public static void main(String[] args) {

    Locale currentLocale;
    ResourceBundle messages;

    currentLocale = new Locale("es");

    messages = ResourceBundle.getBundle("Messages", currentLocale);
    System.out.println(messages.getString("Messagesgreetings"));
    System.out.println(messages.getString("Messagesinquiry"));
    System.out.println(messages.getString("Messagesfarewell"));
    }
}
like image 210
Gabriel Avatar asked Aug 01 '10 18:08

Gabriel


1 Answers

You need to have your locale name in your properties file name.

Rename your properties file to Messages_es.properties

Since you haven't declared any package, both your compiled class file and the properties file can be in the same root directory.

EDIT in response to comments:

Lets say you have this project structure:

test\src\foo\Main.java (foo is the package name)

test\bin\foo\Main.class

test\bin\resources\Messages_es.properties (properties file is in the folder resources in your classpath)

You can run this with:

c:\test>java -classpath .\bin foo.Main

Updated source code:

package foo;
import java.util.Locale; 
import java.util.ResourceBundle;
import javax.swing.JFrame;

public class Main {

  public static void main(String[] args) {

    Locale currentLocale;
    ResourceBundle messages;

    currentLocale = new Locale("es");

    messages = ResourceBundle.getBundle("resources.Messages", currentLocale);
    System.out.println(messages.getString("Messagesgreetings"));
    System.out.println(messages.getString("Messagesinquiry"));
    System.out.println(messages.getString("Messagesfarewell"));
  }
}

Here as you see, we are loading the properties file with the name "resources.Messages"

Hope this helps.

like image 89
Marimuthu Madasamy Avatar answered Oct 16 '22 11:10

Marimuthu Madasamy