Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MissingResourceException: Can't find bundle for base name resources.controls.controls_res, locale en

I can't understand what the problem is, why he swears and can't find locale en. Is there a problem with paths or boundle names?

The legacy project, written 15 years ago, used to be in Ant, now it was translated to Gradle, this error has appeared. It builds on Ant without problems.

P.S. I marked the lines to which the errors refer separately in the classes.

ERRORS:

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()'.

As you can see from the logs, all errors are caused due to the fact that it cannot find * locale en *.

Класс controls_res:

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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  }
}

Class controls_res_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();
  }
}

Class ControlsRes:

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$
  }
}

введите сюда описание изображения

enter image description here

like image 249
Artur Vartanyan Avatar asked Dec 23 '20 11:12

Artur Vartanyan


2 Answers

Gradle does not copy property files while they are in src/main/java folder. You have two options.

1. Option: Add below script into build.gradle. It will run after java compilation and copy all property files into the build directory. Choose this option to avoid changing files positions.

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

Chiriki's answer recommends that (also I agree it) processResources task runs independently although any java codes aren't changed. compileJava.doLast runs only java codes need compile stage, there is a gap for stability. So, choose below script for the first option.

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

2. Option: Move property files into the resources folder src/main/resources. Choose this option for best layout structure. Non-compiling files should be located in the resources folder.

Resources files location layout

like image 152
Ismail Durmaz Avatar answered Oct 06 '22 21:10

Ismail Durmaz


Option 2 in İsmail Durmaz’ answer should be the perfect solution: I would also recommend to move all resources files to the conventional location under src/main/resources. FWIW, this section of the Gradle docs explains how source sets differentiate Java source files from other resources.

However, if you can’t move the properties files to a new location, then a much better solution than İsmail’s Option 1 would be to rather change the processResources task to also copy the properties files from src/main/java:

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

This has the following advantages:

  1. Your compileJava task will only take care of compiling and it can safely decide to be UP-TO-DATE when there are no changes. With İsmail’s answer, the compileJava task will probably always run even if there are no changes: Gradle sees that the output directory of the task is dirty (as there are properties files it doesn’t know about) and hence will rerun the whole compilation to be on the safe side. This is obviously a waste of build time and compute resources.
  2. The resources are copied with the conventional task for this purpose (processResources).
like image 2
Chriki Avatar answered Oct 06 '22 22:10

Chriki