Using Java, How can I get a list of all local users on a windows machine

How can I list all the local users configured on a windows machine (Win2000+) using java. I would prefer doing this with ought using any java 2 com bridges, or any other third party library if possible. Preferable some native method to Java.

Answers


Using a Java-COM Bridge , like Jacob. You then select an appropriate COM library, e.g. COM API for WMI to list local users, or any other Windows management information.

The Win32_SystemUsers association WMI class relates a computer system and a user account on that system.

The Win32_Account abstract WMI class contains information about user accounts and group accounts known to the computer system running Windows. User or group names recognized by a Windows NT domain are descendants (or members) of this class.

Working Example (jacob 1.17-M2, javaSE-1.6):

import java.util.Enumeration;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;

public class ComTst {
public static void main(String[] args) {
    ComThread.InitMTA();
    try {
        ActiveXComponent wmi = new ActiveXComponent("winmgmts:\\\\.");
        Variant instances = wmi.invoke("InstancesOf", "Win32_SystemUsers");
        Enumeration<Variant> en = new EnumVariant(instances.getDispatch());
        while (en.hasMoreElements())
        {
            ActiveXComponent bb = new ActiveXComponent(en.nextElement().getDispatch());
            System.out.println(bb.getPropertyAsString("PartComponent"));
        }
    } finally {
        ComThread.Release();
    }
}
}

Using Java COM Object, i.e. Jacob:

public static void EnumerateUsers() {

    String query = "SELECT * FROM Win32_UserAccount";
    ActiveXComponent axWMI = new ActiveXComponent("winmgmts:\\");
    Variant vCollection = axWMI.invoke("ExecQuery", new Variant(query));
    EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
    Dispatch item = null;
    StringBuilder sb = new StringBuilder();

    while (enumVariant.hasMoreElements()) {
            item = enumVariant.nextElement().toDispatch();
            sb.append("User: " + Dispatch.call(item, "Name")).toString();
            System.out.println(sb);
            sb.setLength(0);
    } 

}

There is a simpler solution for what I needed. This implementation will use the "net user" command to get the list of all users on a machine. This command has some formatting which in my case I don't care about, I only care if my user is in the list or not. If some one needs the actual user list, he can parse the output format of "net user" to extract the list without the junk headers and footers generated by "net use"

private boolean isUserPresent() {
    //Load user list
    ProcessBuilder processBuilder = new ProcessBuilder("net","user");
    processBuilder.redirectErrorStream(true);
    String output = runProcessAndReturnOutput(processBuilder);
    //Check if user is in list
    //We assume the output to be a list of users with the net user
    //Remove long space sequences
    output = output.replaceAll("\\s+", " ").toLowerCase();
    //Locate user name in resulting list
    String[] tokens = output.split(" ");
    Arrays.sort(tokens);
    if (Arrays.binarySearch(tokens, "SomeUserName".toLowerCase()) >= 0){
      //We found the user name
      return true;
    }
    return false;
}

The method runProcessAndReturnOutput runs the process, collects the stdout and stderr of the process and returns it to the caller.


Need Your Help

What happens if I pass in a variable down too many requires()?

javascript node.js performance

I'm passing in the players variable down 3 levels on my NodeJS folder.

MVC3 Site on IIS7.5 - how do I make static content available outside session?

asp.net-mvc-3 iis-7.5 asp.net-mvc-routing http-status-code-404

I have a MVC3 site with static images served nicely from /Content/images to users of the site, but I get a 404 if I try to access them outside a session.