Reputation: 361
I have a web application deployed on my tomcat server. My folder structure is TOMCAT_HOME/webapps/segnalazioni_degrado/config.
The config folder contains some properties files. When I run my application in Development Mode (Eclipse) everything works fine, but since I deployed it on Tomcat I am getting the filenotfound exception. This is the way I load the .properties file on the server:
[...]
Properties props = new Properties();
try {
props.load(new FileInputStream("config/DBconfig.properties"));
} catch (IOException e) {
System.out.println(e.getMessage());
}
url = props.getProperty("url");
user = props.getProperty("user");
password = props.getProperty("password");
[...]
Am I doing something wrong?
Upvotes: 2
Views: 3925
Reputation: 8483
Every webapp has it's own classloader in tomcat. The ClassLoader.getSystemResource
will use the system class loader that is used to load the bootstrap and the tomcat application. But the system class loader does not know anything about your webapp and it's classpath. Using the right class loader is essential.
There are a lot of solutions to access the correct classloader. One solution is to use ClassLoader#getResource.
Please look into this example
Upvotes: 2
Reputation: 309008
Am I doing something wrong?
Obviously, yes. You wouldn't be getting an exception if all was well.
You should load that resource as a stream from the classpath.
That means that your WAR should look like this:
WEB-INF/classes/config/DBconfig.properties
Since you're using Tomcat, I'd recommend learning how to set up a JNDI data source connection pool. Externalize your database connection information that way.
Upvotes: 1