Java – read properties file

I created a rest web service, where I created a config The properties file is used to store and retrieve some user names and passwords through the application I store it in / SRC / main / resources / config Properties

When I try to load it from eclipse from my java code, it works normally But when I deployed it in tomcat, it didn't load The code I use to load the properties file is

properties.load(new FileInputStream("src/main/resources/config.properties"));

Anyone can help me solve this problem

Solution

Deployed to Tomcat is a war file Tomcat does not know or care about the directory where your application comes from

The files in Src / main / resources are copied by Maven to the target / classes directory, and then the directory is copied to the WEB-INF / classes directory of the deployed web application, which is located in the class path of webapp So you just need to use the classloader to load the file:

properties.load(MyClass.class.getResourceAsStream("/config.properties"));

(MyClass is any class of your webapp)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>