转:解决 java.util.MissingResourceException: Can't find bundle for base name com...config, locale zh_CN 错误
2014-11-21 11:04
671 查看
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.
Forinstance,youhaveaprojectlike
Forthisstatementin
Forthisstatementinandincludeconfig.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.
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.propertiesisdirectlyunder
classes,andatthesamelevelas
com.Alternatively,youcanput
config.propertiesintoa
config.jarsuchthat
config.propertiesisattherootof
config.jarwithoutanysubdirectories,andinclude
config.jarintheclasspath.
Forthisstatementin
Scrap.java:ResourceBundleconfig=ResourceBundle.getBundle("com.cheng.scrap.config");towork,youwillneedto
cpsrc\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,
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.
相关文章推荐
- 转:解决 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 configContext, 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 bundle for base name init, locale zh_CN问题的处理
- 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 i18n, locale zh_CN
- 关于:java.util.MissingResourceException: Can't find bundle for base name merchantInfo, locale zh_CN
- 解决 java.util.MissingResourceException: Can't find bundle for base name com...con
- 解决方法:java.util.MissingResourceException Can't find bundle for base name
- MissingResourceException: Can't find resource for bundle java....错误解决
- 错误解决:java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to star
- 【Bug解决】Spring配置错误:java.io.FileNotFoundException: class path resource [cxd/entity/User.hbm.xml] canno
- Caused by: java.lang.IllegalArgumentException: Javassist library is missing in classpath! ..错误解决
- java.util.ConcurrentModificationException错误的解决方法
- java.util.MissingResourceException: Can't find resource for bundle oracle.sysman.db.rsc.LoginResourc
- java.util.NoSuchElementException错误 解决办法
- java.util.MissingResourceException
- [转]java.util.MissingResourceException: Can't find bundle for base name
- java.util.MissingResourceException
- java.util.MissingResourceException