Java: MissingResourceException, "Can't find bundle for base name myMessage, locale zh_CN"

在java中,在多语言国际化时可以用 *.java 类来作为资源文件使用。

1. 首先定义类, 类必须继承ListResourceBundle

类所在路径为: src/I18N

 1 public class myMessage_en_US extends ListResourceBundle {
 2
 3     private final Object mydata[][] = { { "hello", "hello!" },
 4             { "greetting", "Love you . {0}" } };
 5
 6     @Override
 7     protected Object[][] getContents() {
 8         return mydata;
 9     }
10
11 }

2. 编写测试用例,使用资源文件

该文件在default package 包里面

    public static void main() {
        Locale current = new Locale("zh","CN");
        ResourceBundle bundle = ResourceBundle.getBundle("myMessage", current);
        String format = bundle.getString("greetting");
        System.out.println(MessageFormat.format(format, "Jay"));
    }

可惜的是出错了

Exception in thread "main" java.util.MissingResourceException: Can‘t find bundle for base name myMessage, locale zh_CN
    at java.util.ResourceBundle.throwMissingResourceException(Unknown Source)
    at java.util.ResourceBundle.getBundleImpl(Unknown Source)
    at java.util.ResourceBundle.getBundle(Unknown Source)
    at program.I18NTest(program.java:89)
    at program.main(program.java:20)

尝试很多次之后还是不好使,最终把类 myMessage_en_US和调用的main函数都放在了default package下面。好使了

输出结果

你好,Jay

看来是资源路径的问题,经过各种尝试之后把路径进行了修改,如下

private static void I18NTest() {
        Locale current = new Locale("zh","CN");
        ResourceBundle bundle = ResourceBundle.getBundle("I18N.myMessage", current);
        String format = bundle.getString("greetting");
        System.out.println(MessageFormat.format(format, "Jay"));
    }

而不是修改文件存放的路径。

Java: MissingResourceException, "Can't find bundle for base name myMessage, locale zh_CN"

时间: 2024-10-12 12:14:45

Java: MissingResourceException, "Can't find bundle for base name myMessage, locale zh_CN"的相关文章

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name Message, locale zh_CN

问题描述:今天在进行程序国际化编程时,碰到MissingResourceException异常,大意是没有找到属性文件.当时项目目录如下: 我把两个属性文件放到源码包下,所以会出现问题. 解决途径:将属性文件放到src目录下(最好放在与.class同目录下) Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name Message, locale zh_C

java.util.MissingResourceException: Can't find bundle for base name init, locale zh_CN问题的处理

一.问题描述 项目开发使用的是SSM框架,项目那个正常运行,开发一个新功能后,添加了一些配置文件,再重新运行项目抛出异常,找不到name为init的bean. 二.异常信息详细 六月 30, 2018 8:49:56 下午 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() for servlet [springServlet] in context with path [/cyd_online]

Can't find bundle for base name ClientMessages, locale zh_CN

这是个关于JAVA国际化方面的语音包的问题. 提示这个错误信息就是说找不到代码里写的配置文件. 我这个错误发生在导入一个已经存在的项目时发生的. 解决办法:将配置文件*.properties所在的文件夹加入到项目的build path中. 比如我这个项目是将所有的*.properties文件放在config文件夹下的. 那么右击项目属性->Java Build Path->Source->Add Folder加入config文件夹即可. >>>>>>&

转:解决 java.util.MissingResourceException: Can't find bundle for base name com...config, locale zh_CN 错误

Solve java.util.MissingResourceException: Can't find bundle for base name com...config, locale zh_CN at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:836)at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:805)a

Caused by: java.util.MissingResourceException: Can't find bundle for base name javax.servlet.LocalStrings, locale zh_CN

这个是很早以前的一个bug了,最近开始用idea发现追源码相当方便,于是结合网上的解决方案以及自己的判断追踪一下原因,当然没有深究,只是根据提示一直追而已:先说一下解决方案: <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope&g

Can&#39;t find bundle for base name messages, locale zh_HANS_CN

程序没有预计提示信息看起来舒服,在使用hibernate validator提示Can't find bundle for base name messages, locale zh_HANS_CN,www.twitterchina.net而且很多行. 就把hibernate-validator-5.3.4.final.jar中的ValidationMessages.properties配置文件,ValidationMessages_zh_CN.properties配置文件放在src下就OK了.

解决方法 java util MissingResourceException Can&#39;t find bundle fo

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name MessageResource, locale zh_CN at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1427) at java.util.ResourceBundle.getBundl

Can‘t find bundle for base name cache

错误信息:Can't find bundle for base name cache Exception in thread "main" java.lang.ExceptionInInitializerError Admin socket not configured; set to port 0 Exception in thread "main" java.lang.ExceptionInInitializerError at org.ofbiz.base.u

java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.base.BaseSelectProvider

解决错误: java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.base.BaseSelectProvider 整合一遍通用mapper结果报了这个错误,是因为Controller层导包错误,替换后解决 原文地址:https://www.cnblogs.com/murmansk/p/11832329.html