31

I was wondering if using:

System.getProperty("user.dir");

to get the absolute path of a folder is the best way to go about it? I am looking to pass my application onto other computers and I need a full proof way of getting the 'home' directory so that I can just add onto the path when I need to use other folders by just doing:

String path = System.getProperty("user.dir");
String otherFolder = path + "\\other";
user207421
  • 305,947
  • 44
  • 307
  • 483
jn025
  • 2,755
  • 6
  • 38
  • 73
  • The root folder is "/". What are you trying to do? – Elliott Frisch Jul 12 '14 at 04:50
  • "user.dir" is the current working directory, not the home directory – Evan Sebastian Jul 12 '14 at 04:52
  • 1
    You seem to be mixing concepts here. `user.dir` is the current directory. The "absolute path" of a `File` object is obtained by `getAbsolutePath()`. – Jim Garrison Jul 12 '14 at 04:54
  • 1
    Once your title is fixed to express what you're really asking, the question becomes nonsensical. `System.getProperty("user.dir")` is not way to get the home directory at all, it is the way to get the current working directory. -1 for causing yourself confusion. – user207421 Jul 12 '14 at 05:10
  • Sorry, not what I meant I guess. Just meant the working directory. Wrong choice of words :S – jn025 Jul 12 '14 at 05:16
  • The question title and actual content is not related. See http://stackoverflow.com/questions/585534/what-is-the-best-way-to-find-the-users-home-directory-in-java for user.home – Jayan Jul 12 '14 at 05:34

3 Answers3

57

way of getting home directory of current user is

String currentUsersHomeDir = System.getProperty("user.home");

and to append path separator

String otherFolder = currentUsersHomeDir + File.separator + "other";

File.separator

The system-dependent default name-separator character, represented as a string for convenience. This string contains a single character, namely separatorChar.

jmj
  • 237,923
  • 42
  • 401
  • 438
  • 2
    It's "fool-proof", not "full proof"; and no I did not downvote. – Jim Garrison Jul 12 '14 at 04:56
  • See "JDK-4787931 : System property "user.home" does not correspond to "USERPROFILE" (win)" mentioned in http://stackoverflow.com/questions/585534/what-is-the-best-way-to-find-the-users-home-directory-in-java – Jayan Jul 12 '14 at 05:34
  • it has been resolved already @Jay – jmj Jul 12 '14 at 06:25
8

"user.dir" is the current working directory, not the home directory It is all described here.

http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html

Also, by using \\ instead of File.separator, you will lose portability with *nix system which uses / for file separator.

Evan Sebastian
  • 1,714
  • 14
  • 20
6

Program to get the current working directory=user.dir

public class CurrentDirectoryExample {

    public static void main(String args[]) {

        String current = System.getProperty("user.dir");
        System.out.println("Current working directory in Java : " + current);
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Chetna Mishra
  • 59
  • 2
  • 4