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

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

2009-05-23 14:51 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.

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");

这样就解决了路径问题,只要属性文件和读取文件在一起就可以了。它们俩放哪倒是无所谓了。呵呵。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐