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.
3 Comments
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?/usr/share/tomcat, and the way you start tomcat on your system that's true, but then somebody installs the lot on a different OS, or the service infra of your OS is changed, and all of a sudden your java app breaks.user.dir is the "User working directory" according to the Java Tutorial, System Properties
Comments
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)
2 Comments
System.getProperty("user.dir") fetches the directory or path of the workspace for the current project
1 Comment
pwd command, but it looks like if you run with e.g. mvn -f /path/to/project, maven will effectively cd /path/to/project internally before running tests.
-Duser.dirflag affectsFileobjects (if they're not given a full path) but notFileOutPutStreamobjects. If you want to ensure the default directory matches for both, you shouldcdbefore you start Java.