The issue here is the call to System.getProperty("user.home")
, which will return different values based on the user that is executing the java application.
If you are starting tomcat using sudo
, then tomcat is running as the root user, which means /root/ will be returned in the user.home property. So, you need to find out the user that is running tomcat and put the file in the directory under that specific user.
An alternative, more reliable approach would be to define those properties in the web.xml file as:
<context-param>
<param-name>db.host</param-name>
<param-value>localhost</param-value>
</context-param>
<context-param>
<param-name>db.username</param-name>
<param-value>someuser</param-value>
</context-param>
<context-param>
<param-name>db.password</param-name>
<param-value>somepassword</param-value>
</context-param>
Then you can access these properties in your servlets like below:
String databaseHost = getServletContext().getInitParameter("db.username");
String databaseUser = getServletContext().getInitParameter("db.username");
String databasePassword = getServletContext().getInitParameter("db.password");