简体   繁体   English

Java:以管理员身份在新的cmd.exe中执行命令

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

I want to execute a command from a Java-application using 我想使用以下命令从Java应用程序执行命令

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. 什么也没发生,因为我需要给用户und pw作为命令的参数。 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. 但是我需要对cmd运行命令,以便以管理员身份启动新的cmd.exe,并询问是否要以admin身份运行cmd.exe。 After agree the command should be run in the admin-cmd. 同意后,该命令应在admin-cmd中运行。 So like this: 像这样:

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

Has anyone an Idea? 有任何想法吗? Thanks in advance! 提前致谢!

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

从Java以Administrator身份启动命令提示符(CMD),以Administrator身份 运行命令提示符 -或以一种愚蠢的方式尝试将其传递给管道:

pw | command

Research building projects and the executable jar file. 研究建筑项目和可执行的jar文件。 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. 通常,在构建项目时,它会为您提供一个命令行,可用于执行位于projects文件夹中dist文件夹中的jar文件。 You can copy and paste the command line in cmd. 您可以将命令行复制并粘贴到cmd中。 But you could also use a bat file, or convert the executable jar file into an exe with the jar2exe converter. 但是您也可以使用bat文件,或通过jar2exe转换器将可执行的jar文件转换为exe。 Not completely sure of the name because i am not posting on my computer. 不能完全确定名称,因为我没有在计算机上发帖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM