问题
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