我正在使用servlet,jsp和java类构建Web应用程序。属性文件无法从Java类访问
。由此java的结构
我想作为文件存储项目配置访问system.properties
文件。
我做什么用FileInputStream
使用这个Java代码
/* generate the properties file objects */
Properties prop = new Properties();
/* generate IO to read from properties file */
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("system.properties");
if (inputStream != null) {
/* load the properties file */
prop.load(inputStream);
的把所有的变量从文件转换成目标越来越文件
host = prop.getProperty("host");
但它显示错误nullpointerexception
我知道属性文件中的参数未加载。
Apr 26, 2015 7:21:14 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [com.domain.servlet.TotalErrorSummaryServlet] in context with path [/MonitoringDashboard] threw exception
java.lang.NullPointerException
任何建议编辑system.properties
文件的加载程序?当我把文件放在WEB-INF文件夹中时。
把'system.properties'放入'WEB-INF/classes'中,并在' getResourceAsStream(“/ system.properties”)' – morgano
尝试'InputStream inputStream = class.getClass()。getC lassLoader()。getResourceAsStream(“WEB-INF/system.properties”);'而不是你的'inputStream'。 –
如果属性文件不可访问,那么当您执行'prop.load()'时,您会在getProperty之后记录主机的值吗? – RaGe