如何解决MissingResourceException:找不到基本名称资源包的资源.controls.controls_res,区域设置
我无法理解问题是什么,为什么他发誓却找不到locale en
。路径或 boundle
名称是否有问题?
15年前写的legacy
项目,原来在 Ant
,现在翻译成Gradle
,出现这个错误。它建立在 Ant
之上,没有任何问题。
错误:
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 *造成的。
Класс 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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
}
类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();
}
}
类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$
}
}
解决方法
İsmail Durmaz’ answer 中的选项 2 应该是完美的解决方案:我还建议将所有资源文件移动到 src/main/resources
下的常规位置。 FWIW,this section of the Gradle docs 解释了源集如何将 Java 源文件与其他资源区分开来。
但是,如果您无法将属性文件移动到新位置,那么 比 İsmail 的选项 1 更好的解决方案是将 processResources
任务更改为 还从src/main/java
复制属性文件:
processResources {
from(sourceSets.main.java.srcDirs) {
include '**/*.properties'
}
}
这样做有以下优点:
- 您的
compileJava
任务将只负责编译,并且在没有更改时它可以安全地决定为UP-TO-DATE
。根据 İsmail 的回答,即使没有更改,compileJava
任务也可能总是运行:Gradle 发现任务的输出目录是脏的(因为有属性文件,它没有知道),因此将重新运行整个编译以确保安全。这显然是在浪费构建时间和计算资源。 - 出于此目的,使用常规任务复制资源 (
processResources
)。
我不明白那些类 control_res
和 control_res_en
在那种情况下的作用。将相应的 *.properties
文件放在适当的位置并通过 ResourceBundle
API 加载它们应该就足够了;这些课程在我看来已经过时了。
当然,对于 Gradle(或 Maven,如果这也是一个选项),必须将属性文件存储为资源(通常在 /src/main/resources
下),或者相应地调整构建过程,否则文件将不会进入最终的 jar ......
但据我了解,真正的问题是由线路引起的
this(controls_res.class.getResourceAsStream(res));
来自 control_res
中的构造函数。 res
的值为 control_res.properties
;这意味着(根据提供的屏幕截图),程序在文件夹 ~/org/opensourcephysics/resources/controls
中查找属性文件(请参阅 here),但从同一个屏幕截图中,这些文件似乎存储在~/org/opensourcephysics/resources/controls/Resource Bundle 'controls_res'
(我不得不说一个相当奇怪的名字,但这就是屏幕截图的样子……)。
令人讨厌的是,如果请求的资源丢失,Class.getResourceAsStream()
只会返回 null
......
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。