I'm running a simple Java program with below directory structure:
MyProject (A project in my Eclipse IDE)
'-- src
'-- Hello.java
In Hello.java I'm printing the value of 'user.dir' System property.
System.out.println(System.getProperty("user.dir"));
Compiled file for my class is getting stored in MyProject\bin
folder.
When I'm running this class from Eclipse (Right click on source file and click on Run As->Java Application), it prints the path up to 'MyProject' folder, i.e. D:\Projects\Workspace\MyProject
in console window.
Then I used the command window to run the same program. This is what I typed on window:
D:\Projects\Workspace\MyProject\bin>java Hello
and output on console is: D:\Projects\Workspace\MyProject\bin
bin
has been added to previous value for user.dir.
Further, to check more, I this time executed the Java command from a different folder on command window:
D:\Projects\Workspace\MyProject>java -classpath D:\Projects\Workspace\MyProject\bin Hello
This time output on command window is: D:\Projects\Workspace\MyProject
This value changes when I changed the folder on command window, and when I'm running the program from Eclipse, the value for user.dir is the project folder. So I would like to understand, what is the basis for deriving the value of 'user.dir'? How does JVM decides, what should be the value for user.dir?