[英]Java webapp can't load .properties from jar
I am building a web app based on Maven multi modules. 我正在构建基于Maven多模块的Web应用程序。 So My project structure is like the following: 所以我的项目结构如下:
/module1
/src/main/java
package/Module1.java
/src/main/resources
conf/i18n_en.properties
/webapp
/src/main/java
package/Web.java
/src/main/resources
conf/i18n_en.properties
/src/main/webapp
I have different properties("i18n_en.properties") for I18N in every module, I load the properties file in code and get the information. 在每个模块中,I18N都有不同的属性(“ i18n_en.properties”),我将属性文件加载到代码中并获取信息。 It seems good running in single module test. 在单模块测试中运行似乎不错。
private ResourceBundle rb;
String filename = BASE_NAME + "_" + LANG + ".properties";
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
if(in == null) {
in = c.getResourceAsStream(filename);
}
if (in == null) {
in = c.getClassLoader().getResourceAsStream(filename);
}
if (in != null) {
rb = new PropertyResourceBundle(in);
}
However, when I started tomcat with IDEA, the program only load the properties in webapp! 但是,当我使用IDEA启动tomcat时,该程序仅在webapp中加载属性! Just cannot find the "Module1" s properties! 只是找不到“ Module1”的属性!
I think it's the problem of loading properties from innner jar. 我认为这是从内部jar加载属性的问题。 For the deployed directory is as following: 对于已部署的目录如下:
/WEB-INF
/classes
/conf/i18n_en.properties (from webapp)
/lib
/Module1.jar (has it's own properties in it)
I was really confused. 我真的很困惑。 I searched every thing related for a long time and i found some thing similar, but I just can't solve the problem. 我搜索了很长一段时间的所有相关内容,发现相似的内容,但我无法解决问题。 I think I have done what they said. 我想我已经按照他们说的做了。 Java WebApp: Loading resource from .jar located in WEB-INF Java WebApp:从位于WEB-INF的.jar加载资源
I don't know where is the problem, There must be some ways to load the properties inner jar. 我不知道问题出在哪里,必须有一些方法可以加载内部jar的属性。 Anyone knows anything , please help me, thank you very much~ 任何人都知道,请帮助我,非常感谢〜
In jar file, you can't use ClassLoader() method to find an element. 在jar文件中,不能使用ClassLoader()方法查找元素。
You can use: 您可以使用:
try {
URL url2 = new URL(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().toString()+your path to file in structre);
InputStream in = url2.openStream();
} catch (URISyntaxException ex) {
Logger.getLogger(PrepareGUI.class.getName()).log(Level.SEVERE, null, ex);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.