How to access a .properties file from my Java Web Service

ぃ、小莉子 提交于 2019-12-20 04:30:41

问题


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\MainPackagePr

ProcurmentWS1 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?


回答1:


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");
}



回答2:


you can place it in somewhere on your class path and under any folder of your .java source files using :

 ResourceBundle bundle1 =         ResourceBundle.getBundle("com.witness.web.license.l10n.VersionsFileName");
    String fileName = bundle1.getString("11.1");  

com.witness.web.license.l10n being your package



来源:https://stackoverflow.com/questions/20607357/how-to-access-a-properties-file-from-my-java-web-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!