2012-08-24 16 views
0

Hello All, 我正在創建一個ResourceBundle來加載屬性文件。我的文件結構看起來像資源包中的java.util.MissingResourceException異常

| --- Main

| 
    ----ResourceBundleLoad.java 

| --resource

| 
    --- resourcebundle.properties 

正常,當我把主類,並在同一個包的屬性文件意味着它會檢索所有的屬性文件中的值。如果我分開這兩個文件意味着它不工作。它引發java.util.MissingResourceException異常。

我的代碼是

private static final String BUNDLE_NAME = "ExternalizedLogMessages"; 
private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME); 

請建議我該如何解決這個問題

回答

2

我使用的類加載器實現這一目標。 的來源是

private static URLClassLoader resourceLoader= null; 

/** 
* Initialize class loader. 
*/ 
static{ 
    ClassLoader currentThreadClassLoader 
    = Thread.currentThread().getContextClassLoader(); 

    //assuming that current path is the project directory 
    try { 
     resourceLoader 
     = new URLClassLoader(new URL[]{new File(".").toURI().toURL()}, 
           currentThreadClassLoader); 
    } catch (MalformedURLException e) { 
     logger.error(e); 
    } 
} 

/** 
* Properties bundle name. 
*/ 
private static final String BUNDLE_NAME = "resource.ExternalizedLogMessages"; //$NON-NLS-1$ 

/** 
* Resource bundle object. 
*/ 
private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle 
     .getBundle(BUNDLE_NAME,Locale.US,resourceLoader);