Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
systemutils
communitycreator

What is SystemUtils.getUserHome() in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

getUserHome() is a staticThis describes the methods in Java that can be called without creating an object of the class method of the SystemUtils class that is used to return the user home directory of the host system as an instance of the File class.

The path to the user home directory is stored as a system property under the name user.home.

How to import SystemUtils

The definition of SystemUtils can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file:


<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.

You can import the SystemUtils class as follows:


import org.apache.commons.lang3.SystemUtils;

Syntax


public static File getUserHome()

Parameters

The method accepts no parameters.

Return value

This method returns the File object pointing to the home directory.

Code

import org.apache.commons.lang3.SystemUtils;
import java.io.File;
public class Main{
public static void main(String[] args){
File homeDir = SystemUtils.getUserHome();
System.out.printf("The absolute path of the home directory is '%s'.", homeDir.getAbsolutePath());
}
}

Explanation

In the above code, we use the getUserHome() method to get the File object pointing to the home directory.

Next, we print the absolute path of the home directory.

Output

The output of the code is as follows:


The absolute path of the home directory is '/Users/educative'.

Running the above code in your system may give different outputs, depending on your machine.

RELATED TAGS

java
systemutils
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring