Java: execute command in new cmd.exe as administrator

I want to execute a command from a Java application using

Runtime.getRuntime.exec(command); 

but the team needs administrator privileges. If i use

 runas /user:Administrator "cmdName parameters" 

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

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

Is there any idea? Thanks in advance!

+8
java cmd batch-file exec
source share
3 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); 
+1
source share

Run command line (CMD) from java as administrator and Run command line as administrator - or a silly way, try it:

 pw | command 
0
source share

Research project projects and jar executable. Usually, when you build a project, it gives you a command line that can be used to execute the jar file in the dist folder located in the projects folder. You can copy and paste the command line into cmd. But you can also use the bat file or convert the jar executable to exe using the jar2exe converter. Not quite sure of the name, because I do not place it on my computer.

0
source share

All Articles