Can't find bundle for base name config, locale en_US

转载 2012年11月04日 19:11:34

[2012-09-29 10:19:33] [INFO] 获取数据库连接::Can't find bundle for base name config, locale en_US

[2012-09-29 10:19:33] [ERROR] SmsSubAccountServiceImpl类中的|activeStatus()方法null

ResourceBundle.getBundle("myresource")

如果发生ResourceBundle.getBundle("myresource") 读取不到资源文件时,需要注意下面问题.

1java project

仅仅需要把myresource.properties文件放在src下,如果是放在package下,则程序的filename应该package/myresource


2j2eeweb project

1myresource.properties要放在WEB-INFclasses下,

2,如果是使用jar来执行读取资源文件时,该jar需要添加在lib下面,不仅仅是由IED(eclipse)配置的classpath

 

解释:getBundle加载资源文件时,会读取当前应用的classLoader,遍历当前的classloader找出classpath,进行资源文件的绑定,如果找不到则会报出

java.util.MissingResourceException

关键在于,如果使用eclipse配置应用,而且是将lib托管给elipse去加载,那么eclipse中启动tomcat时,应用的加载lib下的classloadertomcat读取应用后加载的class就不会是同一个。

tomcat启动时,根据tomcat配置的jre,加载应用。而且应用的lib托管给eclipse,由elipse设定的jre加载lib下的jar。此时就会有2jre环境。

解决方法是:把所有jar放到WEB-INFlib下,由容器自己加载。当然也要在此加载到classpath下,项目编译需要。

 

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

头午写那个程序,人家老大说了,得要配活。。。怎么配活呢。读取一个文本文件,这样就活啦。晕。。。

下午就开始了。。。文件属性及值都搞好了,然而在配属性文件时出现标题的样子错误。然后在网上找呀找。。终于找到了,NND,可惜是英文的,不过俺的英语还能看个八九不离十。。

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");

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

 

Can't find bundle for base name DBInfo, locale zh_CN解决方法

当我们使用java.util库的ResourceBundle类来引入一些我们的配置文件时,如 ResourceBundle rb = ResourceBundle.getBundle("DBInfo...
  • Jexhen
  • Jexhen
  • 2017年04月17日 17:16
  • 3950

关于:java.util.MissingResourceException: Can't find bundle for base name merchantInfo, locale zh_CN

出现如下的报错: java.util.MissingResourceException: Can't find bundle for base name merchantInfo, locale z...
  • xiaofeixia22222
  • xiaofeixia22222
  • 2012年05月27日 16:15
  • 8134

解决方法:java.util.MissingResourceException Can't find bundle for base name

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name Messa...
  • jdsjlzx
  • jdsjlzx
  • 2011年01月22日 12:51
  • 25224

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

记录学习中的错误点1 进行国际化操作的时候遇到如下错误:java.util.MissingResourceException: Can't find bundle for base name java...
  • qzjn123456789
  • qzjn123456789
  • 2013年12月01日 01:56
  • 3056

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

编译ant的时候,ant执行了一个java类,类中在运行数据库的时候,取数据库连接的配置信息在configure.properties文件中存放。执行target的时候,报错:Can't find b...
  • xfblue2dreamfy
  • xfblue2dreamfy
  • 2010年06月11日 10:59
  • 12184

java.util.MissingResourceException: Can't find bundle for base name i18n, locale zh_CN

今天又转到需要用WEB SERVICES来做东西,说实话,自己对这个说不上话,因为,算是第一次接触,可能很快就能够拿来做东西,解决当前的开发问题,不过,还是得慢慢的多学点。按照网上的说(http://...
  • sebatinsky
  • sebatinsky
  • 2010年05月21日 16:44
  • 11968

Can't find bundle for base name javax.servlet.LocalStrings, locale zh_CN错误处理

相信大家在做java开发的时候,都遇到过“Can't find bundle for base name javax.servlet.LocalStrings, locale zh_CN”的错误,我今...
  • kevin_pso
  • kevin_pso
  • 2017年01月05日 17:24
  • 1707

关于SpringMVC MockMvc测试 Can't find bundle for base name javax.servlet.LocalStrings, locale zh_CN错误

先说一下本人的环境: jdk1.7.0_80 ,springMVC4.0 ,meclipse2014,junit4.11 本人在使用spring mvc 自带的MockMVC做单元测试时,遇...
  • u010448530
  • u010448530
  • 2017年02月22日 16:02
  • 798

国际化java.util.MissingResourceException: Can't find bundle for base name message报错

使用myEclipse实现java程序国际化的时候,我遇到java.util.MissingResourceException异常,百度看看发现也很多人遇到同样的问题,可能是我太笨了居然没看懂。文字描...
  • Miss_Jasmine
  • Miss_Jasmine
  • 2016年05月11日 23:29
  • 3169

解决方法:java.util.MissingResourceException Can't find bundle for base name

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name Messa...
  • jinwufeiyang
  • jinwufeiyang
  • 2016年10月19日 14:01
  • 5512
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Can't find bundle for base name config, locale en_US
举报原因:
原因补充:

(最多只允许输入30个字)