MissingResourceException due to class loader constallation (caller not in WEB-INF/classes)

倾然丶 夕夏残阳落幕 提交于 2019-12-25 02:39:10

问题


I get a MissingResourceException when accessing a resource bundle. The problem comes up due to my specific class loader constallation.

I am trying to read a text.properties from within a class which resides under JBOSS_HOME/server/myServer/myDeployDir/myEAR/myJAR.jar (note: the calling class is not in WEB-INF/classes). The text.properties file is here JBOSS_HOME/server/myServer/myDeployDir/myEAR/myWAR/WEB-INF/classes.

When running the following code:

Locale locale = new Locale ("de", "DE");
ResourceBundle rb = ResourceBundle.getBundle(textproperties, locale);

I get the following

java.util.MissingResourceException: Can't find bundle for base name text, locale de_DE.

Unfortunately, I must NOT change the structure of my application. I can neither move the caller class (now in myJAR.jar) to WEB-INF/classes, or can I pack the properties file into myJAR.jar. Is there any way to access the properties file from outside WEB-INF/classes?

The application runs on JBOSS 4.2.3, so I guess I have to keep web-server thread handling in mind.

Thanks for your help in advance, Gunnar


回答1:


You can specify the classloader to use as an additional parameter to getBundle. In a web application this would most likely be the context class loader of the current thread:

ClassLoader cl = Thread.currentThread().getContextClassLoader();
ResourceBundle rb = ResourceBundle.getBundle(textproperties, locale, cl);


来源:https://stackoverflow.com/questions/8560145/missingresourceexception-due-to-class-loader-constallation-caller-not-in-web-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!