Java-以管理员权限执行exe [英] Java - Executing exe with Admin Rights

查看:951
本文介绍了Java-以管理员权限执行exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个研究工具,该工具将几个便携式系统管理工具(主要是系统内部工具)归为一组.我有一个带有JButton的简单框架.

I'm currently developing a study tool which groups several portable system management tools (mainly sysinternal tools). I have a simple frame with a JButton.

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

What am I trying to do? - Along with my java file i have an exe file (for example purposes let's use config.exe) which needs elevated rights to run.

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

After the user clicks on the button how can i do this execute this file?

我只是找到了另一种方式来做到这一点.我从jar文件中制作了一个exe文件,然后转到兼容性"标签,然后选中始终以管理员身份运行".谢谢您的所有帮助.

I just found one other way to do it. I made a exe from my jar file and went to the compatibility tab and checked "Always Run as admin" Thank you for all of your help.

推荐答案

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

First of all locate the directory in which the exe file is located.Then create a text file named as

您的Exe_File_Name" .exe.manifest

"Your_Exe_File_Name".exe.manifest

只需将以下内容放入文件并保存.

Just put the below contents to the file and save it.

  <?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>

现在在您的Java代码中使用它来调用exe.它将以管理员权限"自动调用.

Now use this in your java code to invoke the exe.It will be automatically invoked with Admin Rights.

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
}

我认为这将对您有所帮助.

I think it will be helpful for you.

这篇关于Java-以管理员权限执行exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆