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.

- 1,362
- 1
- 13
- 24
4 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();
}
}
}

- 83,368
- 10
- 76
- 104
-
Can you provide full code example? I'm not familiar with Jacob and COM (The code in other answers is not correct) – kolobok Apr 13 '13 at 17:24
-
Full example not available, maybe a clue like http://stackoverflow.com/questions/1817459/better-code-using-jacob-and-wmi/ will help. Jacob is at http://sourceforge.net/projects/jacob-project/ nowadays. – gimel Apr 14 '13 at 05:35
-
How to get Name property of PartComponent? (Solved: I can replace Win32_SystemUsers to Win32_UserAccount and PartComponent to Name). Also, you should delete "break;" from your code. – kolobok Apr 14 '13 at 12:04
-
@akapelko - break removed. Glad you could use the example ;-) – gimel Apr 14 '13 at 12:39
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);
}
}

- 2,209
- 3
- 27
- 36
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.

- 354,903
- 100
- 647
- 710

- 1,362
- 1
- 13
- 24
import com.sun.jna.platform.win32.Netapi32Util;
Netapi32Util.User[] users = Netapi32Util.getUsers();
for(Netapi32Util.User user : users) {
System.out.println(user.name);
}

- 446
- 6
- 16