使用 Java,如何获取 Windows 机器上所有本地用户的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/333812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Java, How can I get a list of all local users on a windows machine
提问by Alex Shnayder
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.
如何使用java列出在Windows机器(Win2000+)上配置的所有本地用户。
如果可能的话,我更愿意使用任何 java 2 com 桥接器或任何其他第三方库来执行此操作。
最好使用 Java 的一些本机方法。
回答by gimel
Using a Java-COM Bridge , like Jacob. You then select an appropriate COM library, e.g. COM API for WMIto list local users, or any other Windows management information.
使用 Java-COM 桥接器,例如Jacob。然后选择合适的 COM 库,例如用于 WMI 的 COM API以列出本地用户或任何其他 Windows 管理信息。
The Win32_SystemUsersassociation WMI class relates a computer system and a user account on that system.
所述Win32_SystemUsers关联WMI类涉及计算机系统并在该系统上的用户帐户。
The Win32_Accountabstract 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.
该Win32_Account抽象WMI类包含有关已知到运行Windows的计算机系统中的用户帐户和组帐户信息。Windows NT 域识别的用户名或组名是此类的后代(或成员)。
Working Example (jacob 1.17-M2, javaSE-1.6):
工作示例(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();
}
}
}
回答by b1nary.atr0phy
Using Java COM Object, i.e. Jacob:
使用 Java COM 对象,即 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);
}
}
回答by Alex Shnayder
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"
我需要一个更简单的解决方案。
这个实现将使用“ net user”命令来获取机器上所有用户的列表。这个命令有一些格式,在我的情况下我不关心,我只关心我的用户是否在列表中。如果有人需要实际的用户列表,他可以解析“net user”的输出格式来提取列表,而没有“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.
方法 runProcessAndReturnOutput 运行进程,收集进程的 stdout 和 stderr 并将其返回给调用者。