How to get the system user name in java

This example shows you how to get operating system active user’s login name. We can obtain the username of current user by reading system properties using the user.name key.

Below is the example of code which get the active user name of OS.

package com.java.connect.lang;
 
//How to get the system user name in java
public class OSUserNameExample {
    public static void main(String[] args) {
        String username = System.getProperty("user.name");
        System.out.println("The active username is : " + username);
    }
}

This will give output as active username of operating system.