Share this page 

Get the user name (this howto is deprecated)Tag(s): DEPRECATED


In application :
public class Test {
    public static void main(String args[]) {
      System.out.println( System.getProperty("user.name") );
    }
}
will print the current user. You can't use this technique to secure your application since it is very to spoof.

You just need to specify a "user.name" from the command line.

> java -Duser.name=Elvis Test
Elvis

As an alternative with JDK1.5,

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

In Applet there is no way unless you ask for it or use a signed applet. If you have access to a server-side, something like an ASP page can be used to detect the current NT user name if the client and the server are configured correcty (SSO).
See this related HowTo for a JSP hack!