Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the Java VM decides the value of the user.dir System property?

Tags:

java

eclipse

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?

like image 784
Vicky Avatar asked Feb 15 '11 12:02

Vicky


People also ask

What is the value of user dir?

It's the directory where java was run from, where you started the JVM. Does not have to be within the user's home directory. It can be anywhere where the user has permission to run java. So if you cd into /somedir , then run your program, user.

What is system property user dir?

This directory is named by the system property user. dir, and is typically the directory in which the Java virtual machine was invoked. In other words, user. dir is the working directory of the process that started the Java process at the time when it started the process.

What is system property in Java?

System properties include information about the current user, the current version of the Java runtime, and the character used to separate components of a file path name. The following table describes some of the most important system properties.

Where are system properties stored in Java?

properties file located in users->appdata->locallow->sun->java>deployment and also directly putting key=value in runtime parameter in java control panel but not working. Edit: We use jeety server for deployment. And we have many properties file bundled with our souce code. What kind of application is it?


2 Answers

As defined by java.lang.System specification the user.dir property returns the current working directory (i.e. the current directory when JVM was started):

user.dir User's current working directory

I see nothing contradictory in your example. The only thing unclear here is the name of the property. I don't understand why they chose to put 'user' in there.

Similarly if you executed the same Java program from totally different path you would get the other path as the outcome. Try this yourself:

 c:
 cd c:\
 java -cp D:\Projects\Workspace\MyProject\bin Hello

What Eclipse does before running your program is something similar to:

 d:
 cd d:\projects\workspace\myproject
 java -cp d:\projects\workspace\myproject\bin Hello
like image 90
Grzegorz Oledzki Avatar answered Oct 04 '22 21:10

Grzegorz Oledzki


The property user.dir is defined to be the current working directory. The javadoc for System details the various different system properties.

Maybe you actually want user.home?

like image 27
Richard Miskin Avatar answered Oct 04 '22 22:10

Richard Miskin