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

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

2014-11-21 11:04 633 查看
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐