14

I have to run a batch files from a java program which need administrative privilege. I am using the below command

runtime.getruntime().exec("runas /user:Admin \"C:\\Program Files\\test.bat\"");

but when it get executed its prompt for the password for the Admin account. How can I give the password for it.

kapex
  • 28,903
  • 6
  • 107
  • 121
Muzy
  • 505
  • 2
  • 6
  • 16

3 Answers3

11

You can't pipe a password into runas, as it requests the password from the terminal, not from stdin. There's no equivalent of "sudo -s".

user2286013
  • 111
  • 1
  • 2
3

Try this

runtime.getruntime().exec("cmd /C echo YOUR_PASS | runas /user:Admin \"C:\Program Files\test.bat\"");
kapex
  • 28,903
  • 6
  • 107
  • 121
yatul
  • 1,103
  • 12
  • 27
  • File exception java.io.IOException: Cannot run program "echo": – Muzy May 30 '12 at 07:02
  • 1
    I don't have a windows machine to hand, but can you try "cmd.exe /C echo" instead of "echo"? – Simon Nickerson May 30 '12 at 07:10
  • Yep, this helps to call echo cmd. fix this in answer – yatul May 30 '12 at 07:17
  • 1
    @Muzy Keep in mind that when you use `runas`, the initial current folder for the spawned process will change to `%SYSTEM%\system32` – Doc Dec 18 '13 at 22:55
  • 1
    This works? If I try, it doesn't think my password is correct. If I just run "cmd /C echo|set /p=pwd | runas /user:Admin "cmd"" in a command line, it tells me I have the wrong password, when I don't! – Feytality Dec 22 '15 at 20:58
  • 1
    Does not work for me. Piping the password does not work in Windows 7. – Daniel Feb 03 '16 at 13:19
  • 1
    Can we get an edit on this question? The accepted answer is clearly wrong and doesn't work. There is a correct answer below but doesn't give a workaround. – Forbin Feb 03 '20 at 16:35
-1

when i was kid a did this

echo Pa$$W0Rd | some_command_expects_it
triclosan
  • 5,578
  • 6
  • 26
  • 50