Java“user.dir”属性 - 它究竟是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16239130/
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
Java "user.dir" property - what exactly does it mean?
提问by johnny-b-goode
I want to use user.dir
dir as a base dir for my unit tests (that creates a lot of files). Is it correct that this property points to the current working directory (e.g. set by the 'cd' command)?
我想使用user.dir
dir 作为我的单元测试的基本目录(这会创建很多文件)。此属性指向当前工作目录是否正确(例如,由“cd”命令设置)?
采纳答案by whiskeyspider
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.
它java
是运行的目录,您启动 JVM 的目录。不必位于用户的主目录中。它可以在用户有权运行 java 的任何地方。
So if you cd
into /somedir
, then run your program, user.dir
will be /somedir
.
所以如果你cd
进入/somedir
,然后运行你的程序,user.dir
将会是/somedir
。
A different property, user.home
, refers to the user directory. As in /Users/myuser
or /home/myuser
or C:\Users\myuser
.
另一个属性user.home
指的是用户目录。如在/Users/myuser
或/home/myuser
或 中C:\Users\myuser
。
See herefor a list of system properties and their descriptions.
有关系统属性及其说明的列表,请参见此处。
回答by Mark Rotteveel
user.dir
is the "User working directory" according to the Java Tutorial, System Properties
user.dir
根据Java 教程,系统属性,是“用户工作目录”
回答by Evgeniy Dorofeev
Typically this is the directory where your app (java) was started (working dir). "Typically" because it can be changed, eg when you run an app with Runtime.exec(String[] cmdarray, String[] envp, File dir)
通常,这是您的应用程序 (java) 启动的目录(工作目录)。“通常”因为它可以更改,例如当您运行应用程序时Runtime.exec(String[] cmdarray, String[] envp, File dir)
回答by Evgeniy Dorofeev
System.getProperty("user.dir")
fetches the directory or path of the workspace for the current project
System.getProperty("user.dir")
获取当前项目的工作空间的目录或路径