2013-12-11 95 views
0

我有兩個jar文件。其中一個罐子需要從另一個罐子裏讀取語言包。這裏是兩個罐子的基本結構從類路徑加載ResourceBundle

 
jar1  
    |--com.example.BundleLoader 



jar2 
    |--strings 
     |--language.properties 
     |--language_fr.properties 

語言包在類路徑上。爲了通過BundleLoader讀取語言包,我嘗試將ResourseBundle與ClassLoader結合使用。

這是我在嘗試做

<--snipet--> 
private ResourceBundle englishStrings = null; 
private ResourceBundle frenchStrings = null; 
<--end of snippet---> 


<---snippet---> 
     System.out.println("About to load url from classpath for properties file "+bundleLocation); 
     URL url=ClassLoader.getSystemResource(bundleLocation); 
     bundleLocation=url.toString(); 
     System.out.println("Ok loaded it"); 
     System.out.println("url is "+(url==null?"not available":url.toString())); //Got the folder location 

     try { 
      URL[] urls = {new URL(bundleLocation)}; //created array of ULRs to be loaded by URLClassLoader 
      this.englishStrings = ResourceBundle.getBundle("validationStrings", Locale.getDefault(),new URLClassLoader(urls)); //Exception here 
      this.frenchStrings = ResourceBundle.getBundle("validationStrings", Locale.FRENCH,new URLClassLoader(urls));    
      } catch (MalformedURLException e1) 
      {// TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 

    } 
<--End of Snippet--> 

不同的是我得到的是

java.lang.ExceptionInInitializerError 
    at com.example.Model.model.dObject.doCommon(dObject.java:70) 
    at com.example.Model.model.dObject.validate(dObject.java:42) 
    at com.example.Model.model.test.dObjectTest.testChpValidate(dObjectTest.java:49) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 
Caused by: java.util.MissingResourceException: Can't find bundle for base name validationStrings, locale en_CA 
    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1499) 
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1322) 
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:1028) 
    at com.example.util.Strings.<init>(Strings.java:51) 
    at com.example.util.Strings.<clinit>(Strings.java:26) 
    ... 27 more 

基本上我都找不到,即使屬性文件都在包文件夾。有沒有人碰到過這個?我以爲有人曾經處理過這個問題。

謝謝。

回答

0

ResourceBundle.getBundle("validationStrings", Locale.FRENCH)將在類路徑上的任何JAR中查找直接位於根目錄下的名爲validationStrings_fr.properties的文件。如果找不到,它會嘗試validationStrings.properties

如果你不能夠改變語言包JAR的結構,儘量只用

this.englishStrings = ResourceBundle.getBundle("strings.language"); 
this.frenchStrings = ResourceBundle.getBundle("strings.language", Locale.FRENCH); 

englishStrings線替換整個第二個片段會尋找文件strings/language_en_CA.properties(假設你的默認語言環境是en_CA ),然後在找不到時找回strings/language.properties。該frenchStrings行將尋找strings/language_fr.properties

+0

這是一個原始的代碼,沒有工作:( – svager

+0

哎呀劃傷我以前的評論,沒有注意到這些點,他們很重要,我有斜槓,這就是爲什麼它不工作 – svager

+0

我認爲它 –