转:解决 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)
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.

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

时间: 2024-08-04 03:08:15

转:解决 java.util.MissingResourceException: Can't find bundle for base name com...config, 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]

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

解决方法 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

Java: MissingResourceException, &quot;Can&#39;t find bundle for base name myMessage, locale zh_CN&quot;

在java中,在多语言国际化时可以用 *.java 类来作为资源文件使用. 1. 首先定义类, 类必须继承ListResourceBundle 类所在路径为: src/I18N 1 public class myMessage_en_US extends ListResourceBundle { 2 3 private final Object mydata[][] = { { "hello", "hello!" }, 4 { "greetting&quo

java读取package中的properties文件java.util.MissingResourceException

文件结构: /build/classes/d914/Hello.class /build/classes/d914/mess.properties /build/classes/d914/mess_zh_CN.properties /build/classes/d914/mess_en_US.properties 在eclipse中运行如下代码: package d914; import java.util.ResourceBundle; import java.util.Locale; pub

java.util.MissingResourceException解决策

.properties文件放到当前目录下会报错: 需要放到src文件夹下: java.util.MissingResourceException解决策

解决java.util.concurrent.RejectedExecutionException

前言 昨晚12:00运行自动化测试脚本时遇到了java.util.concurrent.RejectedExecutionException这个异常,从异常名称里很容易分析出是提交的任务被线程池拒绝了.查看源码发现是在Activity里,AsyncTask是在自定义的线程池的运行的,但是onDestory函数里确是先显示调用了线程池的shutdown方法,然后才是AsyncTask的cancel操作,因此可能导致任务被拒绝. ThreadPoolExecutor 一个ExecutorServic

java util包概述

util是utiliy的缩写,意为多用途的,工具性质的包这个包中主要存放了:集合类(如ArrayList,HashMap等),随机数产生类,属性文件读取类,定时器类等类.这些类极大方便了Java编程,日常java编程中,经常要用到这些类. 介绍 Java的实用工具类库java.util包.在这个包中,Java提供了一些实用的方法和数据结构.例如,Java提供日期(Data)类.日历 (Calendar)类来产生和获取日期及时间,提供随机数(Random)类产生各种类型的随机数,还提供了堆栈(St