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

How can I list all local users configured on a Windows machine (Win2000 +) using java.
I would prefer to do this using any java combo bridges or any other third party library if possible.
Preferred some native method for Java.

+1


source to share


3 answers


Using Java-COM Bridge like Jacob . Then you select the appropriate COM library for example. COM API for WMI to specify local users or any other Windows management information.

The Win32_SystemUsers WMI relationship refers to 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 Windows computer system. 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();
    }
}
}

      

+3


source


Using a 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);
    } 

}

      

+2


source


There is a simpler solution for what I need.
This implementation will use the net user " command to get a list of all users on the machine. This command has some formatting which I don't care about in my case, I don't care if my user is on the list or not. If anyone needs an actual list of users, it can parse the "net user" output format to extract the list without 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 runProcessAndReturnOutput method starts a process, collects the stdout and stderr of the process, and returns it to the caller.

0


source







All Articles