在 Vista 上以管理员权限启动 Java 运行时进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1420901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Start Java Runtime Process with Administrator rights on Vista
提问by Markus Lausberg
i want to execute a setup.exe installer which installes a software on vista with java 1.6.
我想执行一个 setup.exe 安装程序,它使用 java 1.6 在 vista 上安装一个软件。
The user is not an administrator. When i try to start the process i get the error message:
用户不是管理员。当我尝试启动该过程时,我收到错误消息:
CreateProcess error=740
which indicates, that the user has not enough rights for starting the process.
这表明,用户没有足够的权限来启动进程。
Can i submit a flag or an option to indicate, the the process should execute with administrator rights? Vista itself does have this functionality inside the menu toolbar. Can i use this function in Java.
我可以提交一个标志或一个选项来表明该进程应该以管理员权限执行吗?Vista 本身在菜单工具栏中确实具有此功能。我可以在 Java 中使用这个函数吗?
I call the following code
我调用以下代码
Runtime rt = Runtime.getRuntime();
Process process;
try {
String fileToExecute = new File(mFolder, mSetupFiles[0]).getCanonicalPath();
if (logger.isDebugEnabled()) {
logger.debug("Execute runtime process");
}
process = rt.exec(fileToExecute, null, mFolder);
process.getErrorStream().close();
process.getInputStream().close();
process.getOutputStream().close();
if (logger.isDebugEnabled()) {
logger.debug("Wait until process is finished");
}
process.waitFor();
} catch (IOException e) {
throw new StartException(e);
} catch (InterruptedException e) {
throw new StartException(e);
}
采纳答案by Markus Lausberg
After 2 days of testing i found the following solution.
经过2天的测试,我找到了以下解决方案。
The error comes up when the Vista UAC functionality is activated. UAC shows a question dialog everytime, when a process needs administrator rights.
激活 Vista UAC 功能时会出现此错误。当进程需要管理员权限时,UAC 每次都会显示一个问题对话框。
Showing this dialog causes the issue.
显示此对话框会导致问题。
Instead of using the old
而不是使用旧的
process = rt.exec(fileToExecute, null, mFolder);
command, i am using now the new 1.5 ProcessBuildercommand
命令,我现在使用的是新的 1.5 ProcessBuilder命令
EDIT:
编辑:
To avoid the problem you have to open a command window which requests the permission. And than you have to call the external process.
为了避免这个问题,你必须打开一个请求权限的命令窗口。而不是您必须调用外部进程。
ProcessBuilder builder = new ProcessBuilder(new String[] { "cmd.exe", "/C", fileToExecute });
Also described here Execute an external Program
此处还描述了执行外部程序

