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

前端 未结 4 1300
你的背包
你的背包 2020-12-21 18:17

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

相关标签:
4条回答
  • 2020-12-21 18:51

    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.

    0 讨论(0)
  • 2020-12-21 18:57

    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();
        }
    }
    }
    
    0 讨论(0)
  • 2020-12-21 19:02
    import com.sun.jna.platform.win32.Netapi32Util;
    
        Netapi32Util.User[] users = Netapi32Util.getUsers();
        for(Netapi32Util.User user : users) {
            System.out.println(user.name);
        }
    
    0 讨论(0)
  • 2020-12-21 19:10

    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);
        } 
    
    }
    
    0 讨论(0)
提交回复
热议问题