Java - 使用管理员权限执行exe

Âng*_*elo 2 java admin-rights

我目前正在开发一个学习工具,其中组合了几个便携式系统管理工具(主要是 sysinternal 工具)。我有一个带有 JButton 的简单框架。

我想做什么?- 除了我的java文件之外,我还有一个exe文件(例如,让我们使用config.exe),它需要提升的权限才能运行。

用户单击按钮后,我该如何执行该文件?

编辑:我刚刚找到了另一种方法。我从我的 jar 文件制作了一个 exe,然后转到兼容性选项卡并选中“始终以管理员身份运行”感谢您的所有帮助。

Shi*_*oft 5

首先找到exe文件所在的目录。然后创建一个文本文件,命名为

“Your_Exe_File_Name”.exe.manifest

只需将以下内容放入文件并保存即可。

  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <assemblyIdentity version="1.1.1.1"
   processorArchitecture="X86"
   name="MyApp.exe"
   type="win32"/>
  <description>elevate execution level</description>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
  <security>
   <requestedPrivileges>
    <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
   </requestedPrivileges>
  </security>
  </trustInfo>
 </assembly>
Run Code Online (Sandbox Code Playgroud)

现在在您的 java 代码中使用它来调用 exe。它将使用管理员权限自动调用。

Process process = new ProcessBuilder("C:\\PathToExe\\MyExe.exe","param1","param2",).start();
InputStream is = process.getInputStream();//Get an inputstream from the process which is being executed
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);//Prints all the outputs.Which is coming from the executed Process
}
Run Code Online (Sandbox Code Playgroud)

我想这对你会有帮助。