Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 633
Muten Roshi Avatar asked Oct 04 '13 07:10

Muten Roshi


People also ask

How do I run cmd exe as administrator?

You can open cmd as an administrator by searching for it in the Windows search bar located in the bottom left corner of the desktop screen. Then, right-click on Command Prompt and select Run as administrator.

How do you execute a command in Java?

Executing a Command from String Process process = Runtime. getRuntime(). exec("ping www.stackabuse.com"); Running this code will execute the command we've supplied in String format.


1 Answers

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);
like image 88
Ateeq Avatar answered Sep 24 '22 17:09

Ateeq