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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:57:09  来源:igfitidea点击:

Java "user.dir" property - what exactly does it mean?

java

提问by johnny-b-goode

I want to use user.dirdir 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.dirdir 作为我的单元测试的基本目录(这会创建很多文件)。此属性指向当前工作目录是否正确(例如,由“cd”命令设置)?

采纳答案by whiskeyspider

It's the directory where javawas 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 cdinto /somedir, then run your program, user.dirwill be /somedir.

所以如果你cd进入/somedir,然后运行你的程序,user.dir将会是/somedir

A different property, user.home, refers to the user directory. As in /Users/myuseror /home/myuseror 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.diris 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")获取当前项目的工作空间的目录或路径