0

I applied following codes to change my Computer password but it didn't work:

public static void main(String[] args) throws IOException {    
    Runtime.getRuntime().exec("net user admin password");
}

I applied this "net user admin password" in command prompt as administrator and it worked. I think i need administrator permission to successfully run this code.Please give me solution in step by step and in details Because I'm not so good.Thanks in advance.

Todd
  • 30,472
  • 11
  • 81
  • 89
Ankon
  • 43
  • 9

2 Answers2

0

Quoting from microsoft documents:

To change a user's password at the command prompt, log on as an administrator and type: net user user_name * /domain

So you should run your program as administrator or have logged in as an administrator already.

Note that there is a high restriction on the user type to perform this very critical command cause you can change (or set) a password without typing the old one!!

Non-administrators receive a "System error 5 has occurred. Access is denied" error message when they attempt to change the password.

Note: As Stephen C. said you can run your command with runas instead of run but it requires to enter the password (not in the command line though) so I suggest you to run a batch file instead and run a elevated shortcut inside it, for more details look at this.

Community
  • 1
  • 1
Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
0

According to the answers to "java-running executable with admin privilege", the solution is to use the runas command to run the net command with elevated privilege; i.e. something like this:

Runtime.getRuntime().exec("runas net user admin password");

The Microsoft documentation for runas is here.

This should only work from an admin user account.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216