[英]Why I cant read properties file using getResourceAsStream when it is in classpath (under WEB-INF/lib)
I was testing out reading a properties file using getResourceAsStream when the properties file is in classpath (under WEB-INF\\lib folder). 当属性文件位于类路径(在WEB-INF \\ lib文件夹下)时,我正在测试使用getResourceAsStream读取属性文件。 And it is not working for some reason.
由于某种原因,它不起作用。 Can anybody tell me what I am doing wrong?
有人可以告诉我我做错了吗?
This is my environment: Operating system is Windows 7. IDE is Eclipse 4.2.3 Kepler 64-bit. 这是我的环境:操作系统是Windows7。IDE是Eclipse 4.2.3 Kepler 64位。 Server is Tomcat 6.0.37 running on 8081 port.
服务器是在8081端口上运行的Tomcat 6.0.37。
I created a web application MyWebProject . 我创建了一个Web应用程序MyWebProject 。
I have a app.properties
file which is located in WEB-INF\\lib
folder. 我有一个位于
WEB-INF\\lib
文件夹中的app.properties
文件。 This is the content of app.properties
file: 这是
app.properties
文件的内容:
DataSource=jdbc/MyDB
数据源= JDBC / MYDB
I created a class AppProperties
which reads this file. 我创建了一个
AppProperties
类,它读取该文件。 This is the class: 这是课程:
public final class AppProperties {
private static String DATA_SOURCE_NAME;
public static String getDataSourceName() {
return DATA_SOURCE_NAME;
}
static {
ClassLoader classLoader = null;
InputStream propertiesFile = null;
Properties properties = null;
final String PROPERTIES_FILE_NAME = "app.properties";
properties = new Properties();
classLoader = Thread.currentThread().getContextClassLoader();
System.out.println("AppProperties: Name of classLoader = " + classLoader.getClass().getName());
propertiesFile = classLoader.getResourceAsStream(PROPERTIES_FILE_NAME);
System.out.println("AppProperties: propertiesFile = " + propertiesFile);
}
}
Then I created a Servlet AppPropertiesServlet
to test it out: 然后,我创建了一个Servlet
AppPropertiesServlet
进行测试:
public class AppPropertiesServlet extends HttpServlet {
public AppPropertiesServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String dataSourceName = null;
dataSourceName = AppProperties.getDataSourceName();
}
}
I deployed the web application MyWebProject on Tomcat. 我在Tomcat上部署了Web应用程序MyWebProject 。 So under
webapps
folder of Tomcat, a new folder MyWebProject
was created with all the necessary files. 因此,在Tomcat的
webapps
文件夹下,使用所有必需的文件创建了一个新文件夹MyWebProject
。 I then run the servlet http://localhost:8081/MyWebProject/AppPropertiesServlet
and this is what appears on Tomcat console: 然后,我运行servlet
http://localhost:8081/MyWebProject/AppPropertiesServlet
,这就是Tomcat控制台上显示的内容:
AppProperties: Name of classLoader = org.apache.catalina.loader.WebappClassLoader
AppProperties:classLoader的名称= org.apache.catalina.loader.WebappClassLoader
AppProperties: propertiesFile = nullAppProperties:propertiesFile = null
Why the propertiesFile is null? 为什么propertiesFile为null? The
app.properties
file is in the WEB-INF\\lib
folder of MyWebProject
folder but somehow the classloader is unable to find it. 该
app.properties
文件在WEB-INF\\lib
的文件夹MyWebProject
文件夹,但不知何故类加载器是无法找到它。 Strange. 奇怪。 What I am doing wrong here?
我在这里做错了什么?
Thanks 谢谢
If you want it to be under WEB-INF/lib
it has to be packaged inside the JAR. 如果您希望它位于
WEB-INF/lib
则必须将其包装在JAR中。
If you want it to be like a normal file , it has to be inside WEB-INF/classes
. 如果您希望它像普通文件一样,则必须位于
WEB-INF/classes
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.