What is SystemUtils.getUserName() in Java?

Overview

getUserName() is a staticdescribes the methods in Java that can be called without creating an object of the class method of the SystemUtils class which is used to return the user name.

The user name is stored as a system property under the name user.name in Java.

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 String getUserName()

Parameters

The method accepts no parameters.

Return value

This method returns the user name.

Code

import org.apache.commons.lang3.SystemUtils;
public class Main{
public static void main(String[] args){
String userName = SystemUtils.getUserName();
System.out.printf("The output of SystemUtils.getUserName() is '%s'.", userName);
}
}

In the above code, we print the user name returned by the getUserName() method.

Output

The output of the code is as follows:


The output of SystemUtils.getUserName() is 'educative'.

Free Resources

Attributions:
  1. undefined by undefined