Java VM如何决定user.dir系统属性的值? [英] How does the Java VM decides the value of the user.dir System property?

查看:156
本文介绍了Java VM如何决定user.dir系统属性的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  MyProject(我的Eclipse IDE中的一个项目)
' - src
' - Hello.java

在Hello.java我正在打印'user.dir'系统属性的值。

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

我的类的编译文件被存储在 MyProject\bin 文件夹。



当我从Eclipse运行这个类(右键单击源文件并单击Run As-> Java Application),它打印直到MyProject文件夹的路径,即 D:\Projects\Workspace\MyProject 在控制台窗口中。



然后我用命令窗口运行相同的程序。这是我在窗口上键入的:

  D:\Projects\Workspace\MyProject\bin> java Hello 

并在控制台上输出: D:\Projects\Workspace\ MyProject\bin



bin 已添加到user.dir的上一个值



此外,为了检查更多,我这次从命令窗口中的另一个文件夹执行Java命令:

  D:\Projects\Workspace\MyProject> java -classpath D:\Projects\Workspace\MyProject\bin Hello 

这个命令窗口输出的时间是: D:\Projects\Workspace\MyProject



当我在命令窗口中更改文件夹时,此值更改,当我从Eclipse运行程序时,user.dir的值是项目文件夹。所以我想明白,导出user.dir值的依据是什么? JVM如何决定,user.dir的值应该是什么?

解决方案

java.lang.System 规范 user.dir 属性返回当前工作目录(即JVM启动时的当前目录):


user.dir 用户当前工作
目录




在你的例子中我没有看到矛盾。这里唯一不清楚的是房产的名称。我不明白为什么他们选择将'用户'放在那里。



同样,如果从完全不同的路径执行相同的Java程序,您将获得另一个路径结果。尝试一下:

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

Eclipse在运行程序之前做了什么是类似的到:

  d:
cd d:\projects\workspace\myproject
java - cp d:\projects\workspace\myproject\bin你好


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?

解决方案

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

这篇关于Java VM如何决定user.dir系统属性的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆