简体   繁体   English

资源包中的java.util.MissingResourceException

[英]java.util.MissingResourceException in Resource bundle

Hello All, I am creating a ResourceBundle to load the property files. 大家好,我正在创建一个ResourceBundle来加载属性文件。 My file structure looks like 我的文件结构看起来像

| | ---Main - -主要

   |
    ----ResourceBundleLoad.java

| | --Resource --resource

   |
    --- resourcebundle.properties

In normal when i put main class and the property file in same package means it retrieves all the property file values. 正常情况下,当我将main类和属性文件放在同一个包中时,它意味着它将检索所有属性文件值。 If i separate both files means it's not working. 如果我分开这两个文件意味着它不起作用。 It throws java.util.MissingResourceException exception. 它抛出java.util.MissingResourceException异常。

My code is 我的代码是

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

Please suggest me how to solve this problem 请建议我如何解决这个问题

I achieve this by using the class loader. 我通过使用类加载器实现这一点。 the source is 来源是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM