aymankoo
aymankoo

Reputation: 671

Windows password and groups

Is it possible to change user password and/or group using Java.

The user who is running the java code have administrator privileges.

And can using java obtain list of windows users.

Upvotes: 1

Views: 351

Answers (2)

Syon
Syon

Reputation: 7401

An easier way to change a users password is using the Runtime class.

Runtime.getRuntime().exec("net user someUsername somePassword");

And you can add or remove groups using the net localgroup windows command.

Runtime.getRuntime().exec("net localgroup someGroup someUser /add");
Runtime.getRuntime().exec("net localgroup someGroup someUser /delete");

To check if the command was successful or not, the exec method returns a Process object. You can read from it's Error and Input streams to get the commands output.

Process pro = Runtime.getRuntime().exec("net user someUsername somePassword");
BufferedReader bre = new BufferedReader(new InputStreamReader(pro.getErrorStream()));
BufferedReader bri = new BufferedReader(new InputStreamReader(pro.getInputStream()));
pro.waitFor(); //wait for the command to finish
String line;
while((line = bre.readLine()) != null)
    System.out.println(bre.readLine());
while((line = bri.readLine()) != null)
    System.out.println(bri.readLine());

Upvotes: 4

Praneeth Peiris
Praneeth Peiris

Reputation: 2088

There is a console command net user, which can be used to change user passwords.
The command is net user userName newPassword.
use Desktop.getDesktop().open("file name"); // find the path to that net command.
Find a better way to execute if you can.

Upvotes: 0

Related Questions