8

So basically wen I googled on what the "user.dir" property does I got this link to a similar question on stackexchange: Java “user.dir” property - what exactly it means?

So basically the answer given was : 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.

Which is okay, but then I found this other link in the android context: Why does System.getProperty(“user.dir”) return “/” As the documentation says, the user.dir property is the user working directory, which is not necessarily the same as the directory where your apk is placed.

So the first best voted answer in the first link says the directory does not have to be within the users home directory while the other says "it is the user working directory."

Now I am terribly confused and need help on this.

2 Answers 2

10

A home directory is associated with a user (for a user MIke, this is often something like /home/mike).

A working directory is associated with a running process; in a shell this is often printed as part of the prompt, for other processes there is usually no direct way of observing it. A user typically starts a shell session in their home directory but then can move away from it.

The directory where Java is installed is a third, unrelated thing (and the directory in which a java application or library is installed could be yet another, unrelated fourth directory). But as the documentation says,

By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.

In other words, user.dir is the working directory of the process that started the Java process at the time when it started the process. If this was a shell, it is the directory to which the shell user had last switched. If Java was called from another program, e.g. a window manager, it is whatever that program had as its working directory.

3

As previous answer says user.dir is "current user directory" Just look at next examples:
cd /path/to/your/jar
$JAVA_HOME/bin/java -jar myprogram.jar
(user.dir = /path/to/your/jar)

cd $JAVA_HOME/bin
java -jar /path/to/your/jar/myprogram.jar
(user.dir = $JAVA_HOME/bin)

cd ~
$JAVA_HOME/bin/java -jar /path/to/your/jar/myprogram.jar
(user.dir = user home directory)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.