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

巧了我就是萌 提交于 2019-12-04 03:26:44
Marimuthu Madasamy

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!