missingresourceexception:找不到基本名称resources.controls.controls\u res、locale en的捆绑包

2w3kk1z5  于 2021-06-29  发布在  Java
关注(0)|答案(3)|浏览(484)

我不明白问题是什么,为什么他发誓找不到 locale en . 路径或路径有问题吗 boundle 名字?
这个 legacy 这个项目写在15年前,曾经是 Ant ,现在它被翻译成 Gradle ,出现此错误。它建立在 Ant 没有问题。
p、 我在类中分别标记了错误所指的行。
错误:

java.lang.ExceptionInInitializerError
    at org.opensourcephysics.controls.OSPLog.<init>(OSPLog.java:937)
    at org.opensourcephysics.controls.OSPLog.getOSPLog(OSPLog.java:124)
    at org.opensourcephysics.cabrillo.tracker.Tracker.loadPreferences(Tracker.java:1391)
    at org.opensourcephysics.cabrillo.tracker.Tracker.<clinit>(Tracker.java:251)
Caused by: java.util.MissingResourceException: Can't find bundle for base name org.opensourcephysics.resources.controls.controls_res, locale en
Caused by: java.util.MissingResourceException: Can't find bundle for base name org.opensourcephysics.resources.controls.controls_res, locale en

    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1581)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1396)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:854)
    at org.opensourcephysics.controls.ControlsRes.<clinit>(ControlsRes.java:55)
    ... 4 more
Caused by: java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:434)
    at java.util.Properties.load0(Properties.java:353)
    at java.util.Properties.load(Properties.java:341)
    at java.util.PropertyResourceBundle.<init>(PropertyResourceBundle.java:138)
    at org.opensourcephysics.resources.controls.controls_res.<init>(controls_res.java:32)
    at org.opensourcephysics.resources.controls.controls_res.<init>(controls_res.java:23)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at java.util.ResourceBundle$Control.newBundle(ResourceBundle.java:2662)
Caused by: java.lang.NullPointerException

    at java.util.ResourceBundle.loadBundle(ResourceBundle.java:1518)
    at java.util.ResourceBundle.findBundle(ResourceBundle.java:1482)
    at java.util.ResourceBundle.findBundle(ResourceBundle.java:1436)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1370)
    ... 6 more
Exception in thread "main" 
Execution failed for task ':Tracker.main()'.

从日志中可以看出,所有错误都是由于找不到locale en而导致的。
класс 控制\u资源:

public class controls_res extends PropertyResourceBundle {
  // relative path to strings
  static String res = "controls_res.properties"; //$NON-NLS-1$

  /**
   * Constructor tools
   * @throws IOException
   */
  public controls_res() throws IOException {
    this(controls_res.class.getResourceAsStream(res)); 23 STRING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  }

  /**
   * Constructor tools
   * @param stream
   * @throws IOException
   */
  public controls_res(InputStream stream) throws IOException {
    super(stream);  // 32 STRING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  }
}

类控件\u res \u en:

/**
 * English resource loader for OSP controls class.  Resource strings are obtained from superclass.
 * @author Wolfgang Christian

* /

public class controls_res_en extends controls_res {
  /**
   * Constructor controls_res_en
   * @throws IOException
   */
  public controls_res_en() throws IOException {
    super();
  }
}

类控件:

public class ControlsRes {
  // static constants for speed
  public static String ANIMATION_NEW;
  public static String ANIMATION_INIT;
  public static String ANIMATION_STEP;
  public static String ANIMATION_RESET;
  public static String ANIMATION_START;
  public static String ANIMATION_STOP;
  public static String ANIMATION_RESET_TIP;
  public static String ANIMATION_INIT_TIP;
  public static String ANIMATION_START_TIP;
  public static String ANIMATION_STOP_TIP;
  public static String ANIMATION_NEW_TIP;
  public static String ANIMATION_STEP_TIP;
  public static String CALCULATION_CALC;
  public static String CALCULATION_RESET;
  public static String CALCULATION_CALC_TIP;
  public static String CALCULATION_RESET_TIP;
  public static String XML_NAME;
  public static String XML_VALUE;
  static final String BUNDLE_NAME = "org.opensourcephysics.resources.controls.controls_res"; //$NON-NLS-1$
  static ResourceBundle res;

  // private constructor because all methods are static
  private ControlsRes() {}

  static {
    String language = Locale.getDefault().getLanguage();
    Locale resourceLocale = Locale.ENGLISH;
    for(Locale locale : OSPRuntime.getInstalledLocales()) {
      if(locale.getLanguage().equals(language)) {
        resourceLocale = locale;
        break;
      }
    }
    res = ResourceBundle.getBundle(BUNDLE_NAME, resourceLocale); // 55 String!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    setLocalStrings();
  }

  private static String getString(final ResourceBundle bundle, final String key) {
    try {
      return bundle.getString(key);
    } catch(final MissingResourceException ex) {
      return '|'+key+'|';
    }
  }

  public static void setLocale(Locale locale) {
    res = ResourceBundle.getBundle(BUNDLE_NAME, locale);
    setLocalStrings();
  }

  /**
   * Gets the localized value of a string. If no localized value is found, the
   * key is returned surrounded by exclamation points.
   *
   * @param key the string to localize
   * @return the localized string
   */
  static public String getString(String key) {
    try {
      return res.getString(key);
    } catch(MissingResourceException ex) {
      return "!"+key+"!"; //$NON-NLS-1$ //$NON-NLS-2$
    }
  }

  /**
  * Gets the local strings.  Static strings are used for speed to avoid having to call the resource object.
  */
  private static void setLocalStrings() {
    ANIMATION_NEW = getString(res, "ANIMATION_NEW");                 //$NON-NLS-1$
    ANIMATION_INIT = getString(res, "ANIMATION_INIT");               //$NON-NLS-1$
    ANIMATION_STEP = getString(res, "ANIMATION_STEP");               //$NON-NLS-1$
    ANIMATION_RESET = getString(res, "ANIMATION_RESET");             //$NON-NLS-1$
    ANIMATION_START = getString(res, "ANIMATION_START");             //$NON-NLS-1$
    ANIMATION_STOP = getString(res, "ANIMATION_STOP");               //$NON-NLS-1$
    ANIMATION_RESET_TIP = getString(res, "ANIMATION_RESET_TIP");     //$NON-NLS-1$
    ANIMATION_INIT_TIP = getString(res, "ANIMATION_INIT_TIP");       //$NON-NLS-1$
    ANIMATION_START_TIP = getString(res, "ANIMATION_START_TIP");     //$NON-NLS-1$
    ANIMATION_STOP_TIP = getString(res, "ANIMATION_STOP_TIP");       //$NON-NLS-1$
    ANIMATION_NEW_TIP = getString(res, "ANIMATION_NEW_TIP");         //$NON-NLS-1$
    ANIMATION_STEP_TIP = getString(res, "ANIMATION_STEP_TIP");       //$NON-NLS-1$
    CALCULATION_CALC = getString(res, "CALCULATION_CALC");           //$NON-NLS-1$
    CALCULATION_RESET = getString(res, "CALCULATION_RESET");         //$NON-NLS-1$
    CALCULATION_CALC_TIP = getString(res, "CALCULATION_CALC_TIP");   //$NON-NLS-1$
    CALCULATION_RESET_TIP = getString(res, "CALCULATION_RESET_TIP"); //$NON-NLS-1$
    XML_NAME = getString(res, "XML_NAME");                           //$NON-NLS-1$
    XML_VALUE = getString(res, "XML_VALUE");                         //$NON-NLS-1$
  }
}


7rtdyuoh

7rtdyuoh1#

gradle不会在属性文件处于 src/main/java 文件夹。你有两个选择。
1.选项:将下面的脚本添加到 build.gradle . 它将在java编译之后运行,并将所有属性文件复制到build目录中。选择此选项可避免更改文件位置。

compileJava.doLast {
    copy {
        from "src/main/java"
        include "**/*.properties"
        into "$buildDir/classes/java/main"
    }
}

chiriki的回答建议(我也同意) processResources 任务独立运行,尽管任何java代码都没有更改。 compileJava.doLast 只运行java代码需要编译阶段,稳定性有差距。因此,选择下面的脚本作为第一个选项。

processResources {
    from(sourceSets.main.java.srcDirs) {
        include '**/*.properties'
    }
}

2.选项:将属性文件移到资源文件夹中 src/main/resources . 选择此选项可获得最佳布局结构。非编译文件应位于resources文件夹中。

dy2hfwbg

dy2hfwbg2#

我不明白这些课程的作用 control_res 以及 control_res_en 在那种情况下。有相应的 *.properties 并通过 ResourceBundle 应用程序编程接口;在我看来,这些课程已经过时了。
当然,对于gradle(或者maven,如果这也是一个选项的话),必须将属性文件存储为资源(在 /src/main/resources ,通常),或者相应地调整构建过程,否则文件将无法进入最终jar…
但据我了解,真正的问题是线路造成的

this(controls_res.class.getResourceAsStream(res));

从中的构造函数 control_res . res 有价值吗 control_res.properties ; 这意味着(根据提供的屏幕截图),程序在文件夹中查找属性文件 ~/org/opensourcephysics/resources/controls (参见此处),但从同一屏幕截图来看,文件似乎存储在 ~/org/opensourcephysics/resources/controls/Resource Bundle 'controls_res' (我不得不说,这是一个很奇怪的名字,但截图是这样的……)。
糟糕的是 Class.getResourceAsStream() 我会回来的 null 如果请求的资源丢失…

5ktev3wc

5ktev3wc3#

i中的选项2̇smail durmaz的答案应该是完美的解决方案:我还建议将所有资源文件移动到 src/main/resources . fwiw,gradle文档的这一部分解释了源代码集如何区分java源文件和其他资源。
但是,如果您不能将属性文件移动到新位置,那么有一个比我更好的解决方案̇smail的第一个选择是改变 processResources 同时从中复制属性文件的任务 src/main/java :

processResources {
    from(sourceSets.main.java.srcDirs) {
        include '**/*.properties'
    }
}

这有以下优点:
你的 compileJava 任务只负责编译,它可以安全地决定 UP-TO-DATE 当没有变化的时候。与我̇斯梅尔的回答是 compileJava 即使没有任何更改,任务也可能始终运行:gradle看到任务的输出目录是脏的(因为它不知道有属性文件),因此将重新运行整个编译以确保安全。这显然是在浪费构建时间和计算资源。
为此,资源与常规任务一起复制( processResources ).

相关问题