How to extract the path "c:/documents and settings/user" using Java... Is there any method??
Asked
Active
Viewed 955 times
2
-
Are you asking if Java has built in ways to provide you with "Special" directories/files like a user's home directory, or settings location in Windows? – Scanningcrew Nov 04 '09 at 07:08
-
Yeah, i am expecting the same. – i2ijeya Nov 04 '09 at 07:10
-
Regarding your question about the class location, I am not sure: see http://mindprod.com/jgloss/properties.html for all System properties. Remember that, if what you are looking for is not there, you still can pass the information as a custom property (`-DmyProperty=xxx`) – VonC Nov 10 '09 at 09:22
2 Answers
7
System.getProperty("user.home")
should be enough. See here for an example.
public class UserHomeExample
{
public static void main(String[] args)
{
System.out.println("User Home Path: "+ System.getProperty("user.home"));
}
}
Gives:
C:\convert\rajesh\completed>javac UserHomeExample.java
C:\convert\rajesh\completed>java UserHomeExample
User Home Path: C:\Documents and Settings\Administrator

VonC
- 1,262,500
- 529
- 4,410
- 5,250
-
VonC Can we also get current file path (i.e) from the above example can i get the info where UserHomeExamble.class is stored ? – Hariharbalaji Nov 10 '09 at 09:12
-
Take a look at http://stackoverflow.com/questions/8136891/getting-absolute-path-of-a-file-loaded-via-classpath, and pass in the class file name you're looking for. If that doesn't help, ask in another question. – deterb Jan 05 '12 at 20:29
3
The user's home directory is exposed by the JVM as a System property. You can retrieve it (as a String) using this method:
String homeDirectory = System.getProperty("user.home");
If you want the parent directory for all users (as you indicate in the question), just append /..
to this.

Chris R
- 833
- 5
- 14