What's an easy way to get the user directory and user home directory?
Author: Deron Eriksson
Description: This Java tutorial describes how to use SystemUtils to get the user directory and user home directory.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)


The SystemUtils class in the Commons LangS library provides convenient static methods for getting File objects representing the user directory and the user home directory. This is illustrated by the UserDirectoryAndUserHomeDirectory class.

UserDirectoryAndUserHomeDirectory.java

package test;

import java.io.File;

import org.apache.commons.lang.SystemUtils;

public class UserDirectoryAndUserHomeDirectory {

	public static void main(String args[]) throws Exception {

		File userDir = SystemUtils.getUserDir();
		System.out.println("User Directory: " + userDir.getCanonicalPath());

		File userHome = SystemUtils.getUserHome();
		System.out.println("User Home: " + userHome.getCanonicalPath());

	}
}

The console output of UserDirectoryAndUserHomeDirectory is shown here:

Results

User Directory: C:\projects\workspace\testing
User Home: C:\Documents and Settings\Deron