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

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)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:576)

You know java is looking for a properties file in a specific locale.   You may be baffled why java keeps complaining it can't find a properties file that is right there.   A few things to keep in mind when debugging this type of errors:

  1. These resource properties files are loaded by classloader, similar to java classes.   So you need to include them in your runtime classpath.
  2. These resources have fully-qualified-resource-name, similar to a fully-qualified-class-name, excerpt you can't import a resource into your java source file.   Why? because its name takes the form of a string.
  3. ResourceBundle.getBundle("config") tells the classloader to load a resource named "config" with default package (that is, no package).   It does NOT mean a resource in the current package that has the referencing class.
  4. ResourceBundle.getBundle("com.cheng.scrap.config") tells the classloader to load a resource named "config" with package "com.cheng.scrap."  Its fully-qualified-resource-name is "com.cheng.scrap.config"

For instance, you have a project like


C:/ws/netbeans5/scrap>
|    build.xml
+---build
|    /---classes
|        /---com
|            /---cheng
|                /---scrap
|                        Scrap.class
|
+---src
|    /---com
|        /---cheng
|            /---scrap
|                    config.properties
|                    Scrap.java

For this statement in Scrap.java: ResourceBundle config = ResourceBundle.getBundle("config"); to work, you will need to  cp src/com/cheng/scrap/config.properties build/classes/ such that config.properties is directly under classes, and at the same level as com.   Alternatively, you can put config.properties into a config.jar such that config.properties is at the root of config.jar without any subdirectories, and include config.jar in the classpath.

For this statement in Scrap.java: ResourceBundle config = ResourceBundle.getBundle("com.cheng.scrap.config"); to work, you will need to  cp src/com/cheng/scrap/config.properties build/classes/com/cheng/scrap/ such that config.properties is directly under classes/com/cheng/scrap/, and at the same level as scrap.   Alternatively, you can put com/cheng/scrap/config.properties (along with the long subdirectories) into a config.jar, and include config.jar in the classpath.   

You may be wondering why it is made so confusing?   The benefits are two-fold, as I see it:  

  1. Location transparency.   At runtime, config.properties is NOT a file, it's just a a loadable resource.   config.properites may not exist in your project at all, and the person who wrote Scrap.java may have never seen this resource.   A URLClassLoader can find it in a network path or URL at runtime.   This is especially important for server-side components such as EJB, Servlet, JSP, etc, who are normally not allowed to access file systems.   When you ask classloaders for a resource, its physical location becomes irrelevant.
  2. Namespace mechanism.   Having a package allows multiple packages to have resources with the same short name without causing conflicts. This is no different from java packages and xml namespaces.

才知道那个属性文件也要加上路径的。于是又开始新征程。这样为了配活,再来。

static ResourceBundle rb = ResourceBundle.getBundle(ReadSource.class.getPackage().toString().substring(8)+".info");

这样就解决了路径问题,只要属性文件和读取文件在一起就可以了。它们俩放哪倒是无所谓了。呵呵。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
org.apache.jasper.JasperException: java.util.MissingResourceException是一种异常情况,通常发生在使用Java的JSP编译器时。它表示在尝试加载资源时发现了一个缺失异常。 这个异常通常是由于以下几种情况引起的: 1.资源文件缺失:当代码中尝试加载资源文件,比如属性文件或国际化资源文件时,如果文件不存在或路径不正确,就会导致MissingResourceException异常的发生。 2.资源文件格式错误:如果资源文件的格式不正确,比如属性文件的语法错误或国际化资源文件的键值对不成对,也会导致类似的异常产生。 3.资源文件权限问题:如果正在访问的资源文件没有足够的权限进行读取,就可能导致MissingResourceException异常的发生。 解决这个异常可以按照以下几个步骤进行: 1.检查资源文件路径:确保资源文件的路径是正确的,可以使用相对路径或绝对路径。 2.检查资源文件格式:确保资源文件的格式正确,可以使用文本编辑器打开并检查每一行的语法是否正确,并确保键值对的对应关系正确。 3.检查资源文件权限:确保正在访问的资源文件有足够的权限进行读取。可以检查文件权限设置或者尝试更改文件所在位置。 总结:org.apache.jasper.JasperException: java.util.MissingResourceException是一种表示在JSP编译器中尝试加载资源时发现了缺失异常的异常情况。解决这个异常可以检查资源文件的路径、格式和权限设置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值