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

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

2012-03-30 17:21 399 查看
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.Afewthingstokeepinmindwhendebugging
thistypeoferrors:

Theseresourcepropertiesfilesareloadedbyclassloader,similartojavaclasses.Soyouneedtoincludetheminyourruntimeclasspath.
Theseresourceshavefully-qualified-resource-name,similartoafully-qualified-class-name,excerptyoucan'timportaresourceintoyourjavasourcefile.Why?becauseitsnametakestheformofastring.
ResourceBundle.getBundle("config")
tellstheclassloadertoloadaresourcenamed
"config"
withdefaultpackage(thatis,nopackage).ItdoesNOTmean
aresourceinthecurrentpackagethathasthereferencingclass.
ResourceBundle.getBundle("com.cheng.scrap.config")
tellstheclassloadertoloadaresourcenamed
"config"
withpackage
"com.cheng.scrap."
Its
fully-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
cp
src\com\cheng\scrap\config.propertiesbuild\classes\
suchthat
config.properties
isdirectlyunder
classes
,andatthesamelevelas
com
.
Alternatively,youcanput
config.properties
intoa
config.jar
suchthat
config.properties
isattheroot
of
config.jar
withoutanysubdirectories,andinclude
config.jar
intheclasspath.
Forthisstatementin
Scrap.java:ResourceBundleconfig=ResourceBundle.getBundle("com.cheng.scrap.config");
towork,youwillneedto
cp
src\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
,and
includeconfig.jarintheclasspath.[/code]
Youmaybewonderingwhyitismadesoconfusing?Thebenefitsaretwo-fold,asIseeit:

Locationtransparency.Atruntime,config.propertiesisNOTafile,it'sjustaaloadableresource.config.properitesmaynotexistinyourprojectatall,andthepersonwhowroteScrap.javamayhaveneverseenthisresource.AURLClassLoadercan
finditinanetworkpathorURLatruntime.Thisisespeciallyimportantforserver-sidecomponentssuchasEJB,Servlet,JSP,etc,whoarenormallynotallowedtoaccessfilesystems.Whenyouaskclassloadersforaresource,itsphysicallocationbecomes
irrelevant.
Namespacemechanism.Havingapackageallowsmultiplepackagestohaveresourceswiththesameshortnamewithoutcausingconflicts.Thisisnodifferentfromjavapackagesandxmlnamespaces.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐