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

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.propertiesis directly under classes, and at the same level as com.  Alternatively, you can put config.properties into aconfig.jar such that config.properties is at the root of config.jar without any subdirectories, and includeconfig.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.propertiesis directly under classes/com/cheng/scrap/, and at the same level as scrap.  Alternatively, you can putcom/cheng/scrap/config.properties (along with the long subdirectories) into a config.jarand includeconfig.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.
再做翻译。问题已经解决。
主要是在写自己的resource bundle的时候,如果是在定制开源框架的Messages(如jmesa或者ec),必须明确的是resource是用classloader在运行时读入的,也就是不是在读文件,不能将.properties文件放在某个文件夹中,然后在web.xml中param-value写文件夹路径。应该是写class路径,而且不能有.properties后缀。
web配置要细心啊,param-value写文件夹路径的时候不能忘记前面的forward slash(前斜杠),而classPath则不需要这个。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: java.util.missingresourceexceptionJava中的一个异常类,表示找不到所需的资源文件。这通常是由于资源文件的路径或名称错误导致的。要解决此问题,您需要检查资源文件的路径和名称是否正确,并确保它们与您的代码中的引用匹配。如果资源文件确实存在但仍然出现此异常,则可能是由于文件权限或编码问题导致的。 ### 回答2: java.util.MissingResourceExceptionJava编程语言中的一个异常类,用于指示资源文件缺失的情况。 当程序尝试加载一个资源文件(通常是properties文件或者资源束)时,如果资源文件不存在或无法访问,就会抛出MissingResourceException异常。 这个异常通常发生在调用java.util.ResourceBundle类的getBundle()方法时。这个方法用于获取指定名称的资源文件,如果找不到资源文件,就会抛出MissingResourceException异常。 要解决这个异常,需要确保资源文件存在且命名正确,并且可以被程序正确地访问。可以检查以下几个方面: 1. 资源文件的位置:确保资源文件位于正确的目录下。通常,资源文件应该放在与Java类相同的目录中,或者放在一个被设置为资源文件的目录中。 2. 资源文件的命名:资源文件的命名应该与调用getBundle()方法时传入的名称一致。注意大小写敏感的问题。 3. 资源文件的编码:如果资源文件包含非ASCII字符,确保文件的编码方式正确,以便在加载时能够正确解析。 4. 资源文件的访问权限:确保程序有足够的权限读取资源文件。如果资源文件位于受限制的目录或者需要特殊权限访问,需要相应地调整权限设置。 5. 重新编译和重新部署:如果以上步骤都没有问题,可以尝试重新编译和重新部署程序,以确保资源文件最新的版本被正确部署。 总之,当出现java.util.MissingResourceException异常时,需要仔细检查资源文件的位置、命名、编码和访问权限等方面,以确定问题所在,并进行相应的调整和修复。 ### 回答3: java.util.missingresourceexceptionJava中的一个异常类,表示在尝试加载资源文件时找不到特定的资源。 这个异常通常发生在程序中调用了ResourceBundle类的getBundle方法时。ResourceBundle类用于加载国际化的资源文件,包括文本、图像和其他相关资源。在调用getBundle方法时,它会尝试根据指定的包名和资源文件名加载对应的资源文件。如果找不到指定的资源文件,就会抛出java.util.missingresourceexception异常。 这个异常通常有以下几种可能的原因: 1. 资源文件不存在:可能是因为资源文件的名称或路径指定错误,或者根本就没有该资源文件。 2. 类路径问题:可能是因为资源文件不在类路径下,导致无法加载资源文件。 3. 文件编码问题:可能是资源文件编码格式不被支持或不正确,导致无法正确读取资源文件。 4. 权限问题:可能是由于访问资源文件的权限不足,无法读取资源文件。 为了解决这个异常,我们可以按照以下步骤进行排查: 1. 确保资源文件的名称和路径是正确的,并且确保资源文件确实存在。 2. 检查资源文件是否位于类路径下,如果不是,可以尝试将资源文件移动到类路径下,或者通过修改类路径来解决。 3. 确保资源文件的编码格式正确,并与代码中指定的编码一致。 4. 检查当前用户是否有足够的权限来读取资源文件,如果没有,则需要相应地修改权限。 总之,java.util.missingresourceexception异常表示在加载资源文件时找不到特定的资源。通过排查资源文件名称、路径、编码和权限等方面的问题,可以解决这个异常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值