您的位置:首页 > 编程语言 > Java开发

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

2011-12-16 13:28 351 查看
java.util.MissingResourceException:Can'tfindbundleforbasename
2008-09-2415:47
java.util.MissingResourceException:Can'tfindbundleforbasename
2007-08-1321:59
头午写那个程序,人家老大说了,得要配活。。。怎么配活呢。读取一个文本文件,这样就活啦。晕。。。

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

Solvejava.util.MissingResourceException:Can'tfindbundleforbasenamecom...config,localezh_CN
atjava.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:836)
atjava.util.ResourceBundle.getBundleImpl(ResourceBundle.java:805)
atjava.util.ResourceBundle.getBundle(ResourceBundle.java:576)

Youknowjavaislookingforapropertiesfileinaspecificlocale.Youmaybebaffledwhyjavakeepscomplainingitcan'tfindapropertiesfilethatisrightthere.Afewthingstokeepinmindwhendebuggingthistypeoferrors:

Theseresourcepropertiesfilesareloadedbyclassloader,similartojavaclasses.Soyouneedtoincludetheminyourruntimeclasspath.

Theseresourceshavefully-qualified-resource-name,similartoafully-qualified-class-name,excerptyoucan'timportaresourceintoyourjavasourcefile.Why?becauseitsnametakestheformofastring.

ResourceBundle.getBundle("config")
tellstheclassloadertoloadaresourcenamed
"config"
withdefaultpackage(thatis,nopackage).ItdoesNOTmeanaresourceinthecurrentpackagethathasthereferencingclass.

ResourceBundle.getBundle("com.cheng.scrap.config")
tellstheclassloadertoloadaresourcenamed
"config"
withpackage
"com.cheng.scrap."
Itsfully-qualified-resource-nameis
"com.cheng.scrap.config"


Forinstance,youhaveaprojectlike

C:\ws\netbeans5\scrap>

|build.xml

+---build

|\---classes

|\---com

|\---cheng

|\---scrap

|Scrap.class

|

+---src

|\---com

|\---cheng

|\---scrap

|config.properties

|Scrap.java


Forthisstatementin
Scrap.java:ResourceBundleconfig=ResourceBundle.getBundle("config");
towork,youwillneedto
cpsrc\com\cheng\scrap\config.propertiesbuild\classes\
suchthat
config.properties
isdirectlyunder
classes
,andatthesamelevelas
com
.Alternatively,youcanput
config.properties
intoa
config.jar
suchthat
config.properties
isattherootof
config.jar
withoutanysubdirectories,andinclude
config.jar
intheclasspath.

Forthisstatementin
Scrap.java:ResourceBundleconfig=ResourceBundle.getBundle("com.cheng.scrap.config");
towork,youwillneedto
cpsrc\com\cheng\scrap\config.propertiesbuild\classes\
com\cheng\scrap\
suchthat
config.properties
isdirectlyunder
classes
\
com\cheng\scrap\
,andatthesamelevelas
scrap
.Alternatively,youcanput
com\cheng\scrap\
config.properties
(alongwiththelongsubdirectories)intoa
config.jar
,andincludeconfig.jarintheclasspath.[/code]

Youmaybewonderingwhyitismadesoconfusing?Thebenefitsaretwo-fold,asIseeit:

Locationtransparency.Atruntime,config.propertiesisNOTafile,it'sjustaaloadableresource.config.properitesmaynotexistinyourprojectatall,andthepersonwhowroteScrap.javamayhaveneverseenthisresource.AURLClassLoadercanfinditinanetworkpathorURLatruntime.Thisisespeciallyimportantforserver-sidecomponentssuchasEJB,Servlet,JSP,etc,whoarenormallynotallowedtoaccessfilesystems.Whenyouaskclassloadersforaresource,itsphysicallocationbecomesirrelevant.

Namespacemechanism.Havingapackageallowsmultiplepackagestohaveresourceswiththesameshortnamewithoutcausingconflicts.Thisisnodifferentfromjavapackagesandxmlnamespaces.

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

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

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

类别:编程中的异常及解决办法|

|添加到搜藏|分享到i贴吧|浏览(3165)|评论(0)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐