0

I have an older java web application which run as root. I wanted to provide relative path to my code in this old application where I have a file I want to read.

To determine the current path where my code runs I did this.

System.out.println(System.getProperty("user.dir"));

Note : This is a regular web app where code resides in src/

My question is if I run the web app as root I always get '/'.

But on my eclipse if I run the same code I get path for my project root directory. Why is that the case ?

Also this will help me figure out how do I provide relative path when the application runs as root.

Kindly let me know. Thanks in advance.

  • 2
    `user.dir` is the current directory (i.e. what is printed by `pwd` in the shell and contained in the `PWD` environment variable). What the current user is mostly doesn't matter. Not to be confused with `user.home` (which is the current users home directory). – Joachim Sauer Aug 07 '20 at 21:17
  • @JoachimSauer thanks for the reply. I looked at this answer https://stackoverflow.com/questions/16239130/java-user-dir-property-what-exactly-does-it-mean and one of them exactly matches with my testing. That is it gives me the workspace directory and the code runs from there. But when I am running this webapp as root. I get '/' that's the reason I am not able to decide why does it return a different path for root user even though its not running from '/' – hopeIsTheonlyWeapon Aug 07 '20 at 21:28
  • If `user.dir` is `/` then that is the current directory when the application is started. Whatever you use to start it probably doesn't bother changing into what you think the "correct" directory is. The "current directory" isn't a terribly useful value for a server application anyway. It sounds like you have a [XY problem](https://xyproblem.info). What are you trying to achieve? – Joachim Sauer Aug 07 '20 at 21:42
  • ok I got it. Thanks for that. – hopeIsTheonlyWeapon Aug 07 '20 at 21:44

0 Answers0