eclipse Java VM 如何决定 user.dir 系统属性的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5003658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does the Java VM decides the value of the user.dir System property?
提问by Vicky
I'm running a simple Java program with below directory structure:
我正在运行一个具有以下目录结构的简单 Java 程序:
MyProject (A project in my Eclipse IDE)
'-- src
'-- Hello.java
In Hello.java I'm printing the value of 'user.dir' System property.
在 Hello.java 中,我正在打印“user.dir”系统属性的值。
System.out.println(System.getProperty("user.dir"));
Compiled file for my class is getting stored in MyProject\bin
folder.
我的班级的编译文件存储在MyProject\bin
文件夹中。
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.
当我从 Eclipse 运行这个类时(右键单击源文件并单击 Run As->Java Application),它会打印到“MyProject”文件夹的路径,即D:\Projects\Workspace\MyProject
在控制台窗口中。
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
控制台上的输出是: D:\Projects\Workspace\MyProject\bin
bin
has been added to previous value for user.dir.
bin
已添加到 user.dir 的先前值。
Further, to check more, I this time executed the Java command from a different folder on command window:
此外,为了检查更多,我这次从命令窗口上的不同文件夹执行了 Java 命令:
D:\Projects\Workspace\MyProject>java -classpath D:\Projects\Workspace\MyProject\bin Hello
This time output on command window is: D:\Projects\Workspace\MyProject
这次命令窗口上的输出是: 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?
当我在命令窗口中更改文件夹时,这个值会发生变化,当我从 Eclipse 运行程序时,user.dir 的值是项目文件夹。所以我想了解,推导'user.dir'值的基础是什么?JVM 如何决定,user.dir 的值应该是多少?
回答by Grzegorz Oledzki
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):
按照java.lang.System
规范user.dir
定义,该属性返回当前工作目录(即 JVM 启动时的当前目录):
user.dir
User's current working directory
user.dir
用户当前工作目录
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:
同样,如果您从完全不同的路径执行相同的 Java 程序,您将获得另一条路径作为结果。自己试试这个:
c:
cd c:\
java -cp D:\Projects\Workspace\MyProject\bin Hello
What Eclipse does before running your program is something similar to:
Eclipse 在运行您的程序之前所做的类似于:
d:
cd d:\projects\workspace\myproject
java -cp d:\projects\workspace\myproject\bin Hello
回答by Richard Miskin
回答by Pa?lo Ebermann
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.)
显示包层次结构根目录的路径,没有/
开头的路径将相对于您的目录Hello.class
。(当它们在 jar 文件中时它也有效。)
回答by karim
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 :)
我总是试图找出 Java 中的默认属性,并且必须为此编写一个程序。如果网上有东西,那么我可以避免这种麻烦。把它放在这里很好:)
The code:
编码:
public class Test {
public static void main(String[] args) {
Properties prop = System.getProperties();
prop.list(System.out);
}
}
Output for Windows XP
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...
回答by mittalpraveen
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
这是用户工作目录。因此,当您从命令提示符运行它时,您正在更改您的工作目录,而在 eclipse 的情况下,您的工作目录是项目的主目录。参考: - http://download.oracle.com/javase/tutorial/essential/environment/sysprop.html