Monday, 17th October 2011
Follow WikiJava on twitter now. @Wikijava

Get the user name

From WikiJava

Jump to: navigation, search


This article shows two ways to obtain the user name from the system.

Contents

the article

Two methods are shown:

  • the first one uses the System.getProperty() method to obtain the user.name value.
  • the second method uses com.sun.security.auth.module.NTSystem so it's specific to the NT Systems, such as Windows XP

System get property

String username;
username = System.getProperty("user.name")

specific for NT systems (Windows)

public class Test {
  public static void main(String args[]) {
    System.out.println(new com.sun.security.auth.module.NTSystem().getName());
  }
}

Unix

public class Test {
  public static void main(String args[]) {
    System.out.println(new com.sun.security.auth.module.UnixSystem().getUsername());
  }
}

See Also

Real's Java How To

Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page


Comments on wikijava are disabled now, cause excessive spam.