8
votes

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!

3
Seems not to work for me. A new admin-cmd won't pop up :/ - Muten Roshi

3 Answers

1
votes

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);
0
votes

Research building projects and the executable jar file. Usually when you build a project, it gives you a commandline that can be used to execute the jar file in folder dist located in the projects folder. You can copy and paste the command line in cmd. But you could also use a bat file, or convert the executable jar file into an exe with the jar2exe converter. Not completely sure of the name because i am not posting on my computer.