list windows users java
My requirement is to list Windows Users from Java API.
While searching, I came across this link.
http://stackoverflow.com/questions/333812/using-java-how-can-i-get-a-list-of-all-local-users-on-a-windows-machine
This involve use of Jacob, a Java-COM bridge.
Here is a sample code.
Note: This code is from the above link.
The complete code is listed below.
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;
import java.util.ArrayList;
/**
*
* @author Srikrishnan
*/
public class WindowsMgmt {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
WindowsMgmt ob=new WindowsMgmt();
ArrayList<String> s=ob.EnumerateUsers();
System.out.println("_____ List _______");
System.out.println(s);
}
public ArrayList<String> EnumerateUsers() {
ArrayList<String> ob=new ArrayList<String> ();
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();
ob.add(Dispatch.call(item, "Name").toString());
System.out.println(sb);
sb.setLength(0);
}
return ob;
}
}
This code require "windows username" to be checked is, to be passed as run time argument.
while running the code, pass the Jacob API JAR file in classpath argument and full path to the Jacob API DLL file in java.library.path as follows.
java -cp .:/var/www/downloads/jacob-1.17/jacob.jar -Djava.library.path=/var/www/downloads/jacob-1.17 WindowsMgmt
Jacob API shall be obtained here,
http://jaist.dl.sourceforge.net/project/jacob-project/jacob-project/1.17/jacob-1.17.zip
My requirement is to list Windows Users from Java API.
While searching, I came across this link.
http://stackoverflow.com/questions/333812/using-java-how-can-i-get-a-list-of-all-local-users-on-a-windows-machine
This involve use of Jacob, a Java-COM bridge.
Here is a sample code.
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);
}
}
Note: This code is from the above link.
The complete code is listed below.
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;
import java.util.ArrayList;
/**
*
* @author Srikrishnan
*/
public class WindowsMgmt {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
WindowsMgmt ob=new WindowsMgmt();
ArrayList<String> s=ob.EnumerateUsers();
System.out.println("_____ List _______");
System.out.println(s);
}
public ArrayList<String> EnumerateUsers() {
ArrayList<String> ob=new ArrayList<String> ();
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();
ob.add(Dispatch.call(item, "Name").toString());
System.out.println(sb);
sb.setLength(0);
}
return ob;
}
}
This code require "windows username" to be checked is, to be passed as run time argument.
while running the code, pass the Jacob API JAR file in classpath argument and full path to the Jacob API DLL file in java.library.path as follows.
java -cp .:/var/www/downloads/jacob-1.17/jacob.jar -Djava.library.path=/var/www/downloads/jacob-1.17 WindowsMgmt
Jacob API shall be obtained here,
http://jaist.dl.sourceforge.net/project/jacob-project/jacob-project/1.17/jacob-1.17.zip
No comments:
Post a Comment