Java user.dir property - what exactly does it mean?
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.
What is the difference between user.dir and .?
I think that the purpose of "user.dir"
property is the same as pwd command in Linux or GetCurrentDirectory in Windows. I think it is more clear this way because we clearly use pwd
when we want to get the (absolute) path of the current directory. In contrast, .
is a shorthand for the current dir when you are not interested to know it but, rather, just tell the system that we want to use the current path. It is reasonable to use the shorthand in this case. First means current path relative to the root
, second -- current path relative to the current path
.
The difference may be important when you communicate between processes. If you pass a path to another process in your system, it may have a different current dir and, thus, you better prefer the absolute path. On the other hand, if you pass a path to another system, then relative path may be preferred.
Java user.dir property - what exactly does it mean?
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.
Why is the user.dir system property working in Java?
Just because new File(".")
gives the desired answer doesn't mean it's doing what you want it to.
For example, try:
new FileOutputStream("foo.txt").close();
Where does that end up? On my Windows box, even though new File(".").getAbsolutePath()
moves around based on user.dir
, foo.txt
is always created in the original working directory. It strikes me that setting user.dir
such that new File(".")
doesn't refer to the current working directory is just asking for trouble.
user.dir property broken on OSX jdk 1.8.0_111? how about other OS, versions?
user.dir
property isn't broken. Setting it isn't supported, and isn't guaranteed to behave in any specific way. See http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4117557 and the linked tickets:
After careful consideration, we still do not believe that the current behaviour can/should be changed. We have never guaranteed that "user.dir" will be consulted at any given time and unfortunately the jdk has assumed that this property will not change. Ideally we'd introduce the concept of a "read-only" system property to guard against unsupported changes in this and other system properties.
"user.dir", which is initialized during jvm startup, should be used as an
informative/readonly system property, try to customize it via command line
-Duser.dir=xyz will end up at implementation dependend/unspecified behavior.
The current FileSystem implementation heavily depends on the assumption that
we dont have a "chdir" or "chdir" like functionality (like use -Duser.dir=xyz)
that will change the "current user dir" to not the one jvm startup from.
That said, the inconsistent behavior of FileIn/OutputStream is indeed a buggy
behavior (FileIn/OutputStream's open impl goes down to naive open directly
without consulting java File/FileSystem because of the assumption mentioned
above).To change the current implementation to support "customizable" user.dir is
a big deal, lots of classes/lines of change, only do it if we believe it's
really worth doing.
System.getProperty(user.dir) alternative
The C:\wamp\www\myProject
folder is just a random place on your hard disk.
Java supports 3 generic places to look for resources:
the current working directory. this is where your command prompt is and what
System.getProperty("user.dir")
returns, but you cannot rely on that beeing somehow related to any cretain place in the file system, especially not related to the project structure on your own system.You should only use that if your program has a command line interface and looks for some default file name to work with.
the user home This is what you get when calling
System.getProperty("user.home")
. On Unix this resoves to$HOME
and on Windows to%USERPROFILE%
.This is the best place for files changed at runtime or holding user specific content.
the own code location. Resources in the same package as your class are accessed with
getClass().getResource("filenameWithoutPath")
But usually you place resources in a special folder in the application root and access it like this:getClass().getResource("/relative/path/from/src/root/filenameWithoutPath")
.In your IDE this special folder should be
Project/src/main/resources
(according to the maven Standard Directory LayoutThis is appropriate for some global configurations that you change when creating the delivery package.
System.getProperty(user.dir) gives different path in console and web application
How System.getProperty("user.dir") works for different kind of application?
It works exactly the same way in all cases. It returns the "current directory" that was specified by the application that launched the application.
In the case of an interactive shell, it will be the current directory of the shell (or subshell) that launched the application.
In the case of a shell script or a native launcher, it will be the current directory set by the script or the launcher. (For example, when you start Tomcat using the "catalina.sh" script, the script sets the current directory.)
In the case of an application launched by Eclipse, it will be the current directory set by the launch configuration you are using. The default is the project directory, but you can override this in the launch config.
Is there any way to get root directory of web application?
I assume that you mean the root directory of the deployed webapp in the web container.
String path = request.getServletContext().getRealPath("/");
For more details:
- Get the root directory name in Java web application if context and root names are different
However, there are various "traps" with accessing webapp files via the file system. One is that they may be clobbered at any time by a redeployment. A better approach is to access "files" in the deployed webapp using getResourceAsStream.
Related Topics
Lombok Annotations Do Not Compile Under Intellij Idea
Multi-Project Test Dependencies with Gradle
How to Force Selenium Webdriver to Click on Element Which Is Not Currently Visible
Sorting Arraylist in Alphabetical Order (Case Insensitive)
Jackson and Generic Type Reference
How to Inject Authenticationmanager Using Java Configuration in a Custom Filter
Does Setting Java Objects to Null Do Anything Anymore
How to Remove Special Characters from a String
Where Do I Find a Standard Trie Based Map Implementation in Java
Java: How to Do Dynamic Casting of a Variable from One Type to Another
Java Get File Size Efficiently
Best Way to Represent a Fraction in Java
Solve Hibernate Lazy-Init Issue with Hibernate.Enable_Lazy_Load_No_Trans
Using a Custom Truststore in Java as Well as the Default One