I am running a simple Java program with the following directory structure:
MyProject (A project in my Eclipse IDE) '-- src '-- Hello.java
In Hello.java, I am printing the value of the 'user.dir' System property.
System.out.println(System.getProperty("user.dir"));
The compiled file for my class is stored in the MyProject\bin .
When I run this class from Eclipse (right-click on the source file and select "Run as Java Application"), it prints the path to the folder "MyProject", that is, D:\Projects\Workspace\MyProject in the console window.
Then I used a command window to run the same program. This is what I typed in the window:
D:\Projects\Workspace\MyProject\bin>java Hello
and console output: D:\Projects\Workspace\MyProject\bin
bin was added to the previous value for user.dir.
Next, to check more, this time I ran a Java command from another folder in the command window:
D:\Projects\Workspace\MyProject>java -classpath D:\Projects\Workspace\MyProject\bin Hello
This time in the command window: D:\Projects\Workspace\MyProject
This value changes when I change the folder in the command window, and when I run the program from Eclipse, the value for user.dir is the project folder. So I would like to understand what is the basis for getting the value of "user.dir"? How does the JVM decide what the value for user.dir should be?