0

みなさん、こんにちは。プロパティファイルをロードするためのResourceBundleを作成しています。私のファイル構造は次のようになります

| - -主要

   |
    ----ResourceBundleLoad.java

| - リソース

   |
    --- resourcebundle.properties

通常、メインクラスとプロパティファイルを同じパッケージに入れると、すべてのプロパティファイルの値が取得されます。両方のファイルを分離すると、機能しなくなります。java.util.MissingResourceException例外をスローします。

私のコードは

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

この問題を解決する方法を教えてください

4

1 に答える 1

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);
于 2012-08-24T09:30:31.880 に答える