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.
aresourceinthecurrentpackagethathasthereferencingclass.
fully-qualified-resource-nameis
Forinstance,youhaveaprojectlike
Forthisstatementin
Alternatively,youcanput
of
Forthisstatementin
andatthesamelevelasand
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.
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.propertiesisdirectlyunder
classes,andatthesamelevelas
com.
Alternatively,youcanput
config.propertiesintoa
config.jarsuchthat
config.propertiesisattheroot
of
config.jarwithoutanysubdirectories,andinclude
config.jarintheclasspath.
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.propertiesisdirectlyunder
classes
\
com\cheng\scrap\,
andatthesamelevelas
scrap.Alternatively,youcanput
com\cheng\scrap\
config.properties(alongwiththelongsubdirectories)intoa
config.jar,
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.
相关文章推荐
- java.util.MissingResourceException
- 解决 java.util.MissingResourceException: Can't find bundle for base name com...con
- Notes for "java.util.MissingResourceException Can't find bundle for base name"
- java.util.MissingResourceException: Can't find resource for bundle java.util.PropertyResourceBundle,
- JSF java.util.MissingResourceException: Can't find bundle for base name javax.faces.Messages, locale zh_CN
- 解决java.util.MissingResourceException: Can't find bundle for base name configContext, locale zh_CN
- [转]java.util.MissingResourceException: Can't find bundle for base name
- java.util.MissingResourceException
- java.util.MissingResourceException
- 解决方法:java.util.MissingResourceException Can't find bundle for base name
- 转:解决 java.util.MissingResourceException: Can't find bundle for base name com...config, locale zh_CN 错误
- java.util.MissingResourceException
- 转:解决 java.util.MissingResourceException: Can't find bundle for base name com...config, locale zh_CN 错误
- java.util.MissingResourceException: Can't find bundle for base name
- java.util.MissingResourceException: Can't find bundle for base name i18n, locale zh_CN
- Solve java.util.MissingResourceException: Can't find bundle for base name com...config, locale zh_CN
- java.util.MissingResourceException: Can't find resource for bundle oracle.sysman.db.rsc.LoginResourc
- java.util.MissingResourceException: Can't find bundle for base name init, locale zh_CN问题的处理
- java.util.MissingResourceException
- 关于:java.util.MissingResourceException: Can't find bundle for base name merchantInfo, locale zh_CN