开发者

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

I'm running a simple Java program with below directory structure:

MyProject (A project in my Eclipse IDE)
 '-- src
      '-- Hel开发者_开发知识库lo.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?


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


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?


In addition to the other answers (which answer the question completely, I think):

If you are in fact searching a way to get to your class files, don't use this property (or any property at all). Use

System.out.println( Hello.class.getResource("/") );

to show the path to the root of your package-hierarchy, and a path without / at the start would be relative to the directory of you Hello.class. (It works when they are inside a jar file, too.)


This is user working directory. So, when you are running it from command prompt you are changing your working directory whereas in case of eclipse your working directory is project's home directory. Ref: - http://download.oracle.com/javase/tutorial/essential/environment/sysprop.html


Default System properties

I always try to find out the default properties in Java, and have to write a program for this. If there is something online, then I can avoid this hassle. Its good to have it here :)

The code:

public class Test {
    public static void main(String[] args) {
        Properties prop = System.getProperties();
        prop.list(System.out);
    }
}

Output for Windows XP

-- listing properties --
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=C:\Program Files\Java\jre6\bin
java.vm.version=19.0-b09
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
path.separator=;
java.vm.name=Java HotSpot(TM) Client VM
file.encoding.pkg=sun.io
user.country=US
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=Service Pack 3
java.vm.specification.name=Java Virtual Machine Specification
user.dir=C:\workspace\Test
java.runtime.version=1.6.0_23-b05
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=C:\Program Files\Java\jre6\lib\endorsed
os.arch=x86
java.io.tmpdir=C:\DOCUME~1\Name~1\LOCALS~1\Temp\
line.separator=

java.vm.specification.vendor=Sun Microsystems Inc.
user.variant=
os.name=Windows XP
sun.jnu.encoding=Cp1252
java.library.path=C:\Program Files\Java\jre6\bin;.;C:\W...
java.specification.name=Java Platform API Specification
java.class.version=50.0
sun.management.compiler=HotSpot Client Compiler
os.version=5.1
user.home=C:\Documents and Settings\User Name
user.timezone=
java.awt.printerjob=sun.awt.windows.WPrinterJob
file.encoding=Cp1252
java.specification.version=1.6
user.name=User Name
java.class.path=C:\workspace\Test\bin
java.vm.specification.version=1.0
sun.arch.data.model=32
java.home=C:\Program Files\Java\jre6
java.specification.vendor=Sun Microsystems Inc.
user.language=en
awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode, sharing
java.version=1.6.0_23
java.ext.dirs=C:\Program Files\Java\jre6\lib\ext;C:...
sun.boot.class.path=C:\Program Files\Java\jre6\lib\resour...
java.vendor=Sun Microsystems Inc.
file.separator=\
java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport...
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
sun.desktop=windows
sun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+m...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜