我已经使用tomcat成功部署了Java Web服务.该Web服务正在访问配置文件(.Properties),我已将config.properties文件放置在以下目录中
C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ProcurementWS1\WEB-INF\classes\MainPackagePrProcurmentWS1是WAR文件的名称
这是尝试从我的代码访问它的方式:
Properties properties = new Properties(); InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("procwsconfig.properties"); properties.load(is);并且在加载时出现空指针异常
你能告诉我哪里做错了吗?
解决方案将其放在src/config中直接放在源中的config包中,构建后将转到/WEB-INF/classes/config/config.properties
和this.getClass().getResourceAsStream("/config/config.properties");
然后我在具有方法的同一项目中创建了一个服务类.
public static InputStream getConfigAsInputStream(){ return Service.class.getResourceAsStream("/config/config.properties"); }I have deployed my java web service successfully using tomcat. This web service is accessing a configuration file (.Properties) I have placed the config.properties files in the following directory
C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ProcurementWS1\WEB-INF\classes\MainPackagePrProcurmentWS1 is the name of the WAR file
this is how am trying to access it from my code:
Properties properties = new Properties(); InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("procwsconfig.properties"); properties.load(is);And am getting a null pointer exception on load
can you tell me what am doing wrong?
解决方案put it in src/config direct to config package in source , upon build it will go to /WEB-INF/classes/config/config.properties
and this.getClass().getResourceAsStream("/config/config.properties");
and then I created a Service Class in the same project which has a method.
public static InputStream getConfigAsInputStream(){ return Service.class.getResourceAsStream("/config/config.properties"); }
更多推荐
如何从我的Java Web服务访问.properties文件
发布评论