Java: execute command in a new cmd.exe as administrator

batch-filecmdexecjava

I want to execute a command from a Java-application using

Runtime.getRuntime.exec(command);

but the command need Admin-privileges. If I use

runas /user:Administrator "cmdName parameters"

nothing happens because I need to give user und pw as parameter to the command.
But I need to run a command to the cmd, so that a new cmd.exe starts as administrator and asks if I want to run cmd.exe as admin. After agree the command should be run in the admin-cmd. So like this:

String command = "popupNewCmdAsAdminAndRun "batWhichNeedsAdmin.bat" "
Runtime.getRuntime.exec(command);

Has anyone an Idea?
Thanks in advance!

Best Answer

you should do the following

Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "command";
process.StartInfo.Verb = "runas";
process.Start();
process.WaitForExit(60000);