【问题标题】:java.util.MissingResourceException in Resource bundle资源包中的 java.util.MissingResourceException
【发布时间】:2012-08-24 07:06:19
【问题描述】:

大家好, 我正在创建一个 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);

请告诉我如何解决这个问题

【问题讨论】:

    标签: java resourcebundle


    【解决方案1】:

    我通过使用类加载器来实现这一点。 来源是

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-26
      • 2013-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-01
      • 1970-01-01
      相关资源
      最近更新 更多