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)?
4 Answers
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.
So if you cd
into /somedir
, then run your program, user.dir
will be /somedir
.
A different property, user.home
, refers to the user directory. As in /Users/myuser
or /home/myuser
or C:\Users\myuser
.
See here for a list of system properties and their descriptions.
-
3Already read it before posting. But "User working directory" is not a best explanation, hope you agree. Commented Apr 26, 2013 at 14:53
-
Is that mean that on tomcat configuration for all java versions if
user.dir
is pointing to/usr/share/tomcat
and in config we add something like this:file:./config
then it will be pointing to/usr/share/tomcat/config
always? Commented Mar 19, 2019 at 11:21
user.dir
is the "User working directory" according to the Java Tutorial, System Properties
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)
-
A value is needed only during junit test execution by maven system. Commented Apr 26, 2013 at 14:54
-
2and when you change it on the MAC, it breaks things like this test stackoverflow.com/questions/45130661/… :( :( Commented Jul 16, 2017 at 16:10
System.getProperty("user.dir")
fetches the directory or path of the workspace for the current project
-
It normally fetches the current working directory, like you get from the
pwd
command, but it looks like if you run with e.g.mvn -f /path/to/project
, maven will effectivelycd /path/to/project
internally before running tests.– rjmunroCommented Oct 9, 2020 at 15:50
-Duser.dir
flag affectsFile
objects (if they're not given a full path) but notFileOutPutStream
objects. If you want to ensure the default directory matches for both, you shouldcd
before you start Java.