ResourceBundle.getBundle 报错 Can't find bundle for base name 解决方法(eclipse中)

最近在学习ResourseBundle时遇到了“Can't find bundle for base name ”这个错误搞了很久才解决了。

我的config.properties 文件位置在 java工程 根目录下的config中;
java 工程目录结构 如下,
CheersJava(工程目录)
-------src
-------lib
-------target
-------config(config.properties在此目录下)
.classpath

java 代码

package com.cheers.demo;
import java.util.ResourceBundle;
public class CheersResourceDemo {
    public static void main(String[] args) {
        ResourceBundle resourceBundle = ResourceBundle.getBundle("config");
                //urlStr 是config.properties文件中key名称
        System.out.println(resourceBundle.getString("urlStr"));
    }
}

解决方法:
在eclipse中 - CheersJava工程上 右键 -Properties -Java Build Path, 在Libraries选项卡中,点击 “Add Class Folder...” 选择config目录将其添加。
然后运行代码,问题解决。

备注:
用本文编辑器打开.classpath 文件,发现多了一行:<classpathentry kind="lib" path="config"/>


参考文章:http://verran.iteye.com/blog/44357

猜你喜欢

转载自blog.csdn.net/chenyulancn/article/details/83756526