Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - using System.getProperty("user.dir") to get the home directory [duplicate]

Tags:

java

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"; 
like image 207
jn025 Avatar asked Jul 12 '14 04:07

jn025


2 Answers

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.

like image 52
jmj Avatar answered Sep 29 '22 09:09

jmj


"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.

like image 41
Evan Sebastian Avatar answered Sep 29 '22 09:09

Evan Sebastian