[英]Null Reading config file with getResourceAsStream
I have a class that reads a config file from the project root but it is not finding the file. 我有一个从项目根目录读取配置文件的类,但找不到该文件。
My read class is in com.br.xxx.util package 我的读取类在com.br.xxx.util包中
String filename = "config.properties";
input = getClass().getResourceAsStream(filename);
This code returns null even though "config.properties" is in my project root. 即使“ config.properties”在我的项目根目录中,此代码也返回null。 What am I doing wrong? 我究竟做错了什么?
If I use: 如果我使用:
InputStream input = new FileInputStream("C:\\Users\\xxxx\\git\\projectfolder\\project\\config.properties");
configFile.load(input);
It works, but I don't want to use a system path. 它有效,但是我不想使用系统路径。
config.properties must be in your classpath
. config.properties必须在您的classpath
。 when you deploy your project on tomcat, by default WEB-IN/classes
is in classpath
. 当您在tomcat上部署项目时,默认情况下WEB-IN/classes
在classpath
。 Simplest solution is to move config.properties
to WEB-IN/classes
. 最简单的解决方案是将config.properties
移至WEB-IN/classes
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.