System.getProperty("user.home") on Tomcat returns "/usr/share/tomcat7/"

6.9k views Asked by At

My Java Application runs on Tomcat and I want to create files under user home directory (such as "/home/USERNAME/").

Method System.getProperty("user.home") works great on Windows, returning "C:\users\USERNAME\". But on Linux it returns "/usr/share/tomcat7/", on which my application haven't permission. I've tried System.getProperty("user.name") and it returns "tomcat7". I've made a workaround but I'm not satisfied.

So, how do I get the user home directory, even when running on any OS and on Tomcat? Thanks.

1

There are 1 answers

6
Nazgul On BEST ANSWER

These properties i.e: user.* are relative to the user which is used to run tomcat. Normally on windows (local development workstations) we use the logged in user to run tomcat. So you are getting the correct values.(values which you think are correct). But on linux and specifically on productions environments, tomcats is always run using a restricted user account which normally is named as 'tomcat' itself. So there when u hit user.* properties you get the values specific to environment variables set for the user that tomcat is running as. Whats happening with you is the right way it should. You should not rely on these properties for your use.