简体   繁体   English

使用Vista的管理员权限启动Java Runtime Process

[英]Start Java Runtime Process with Administrator rights on Vista

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. Vista本身在菜单工具栏中具有此功能。 Can i use this function in Java. 我可以在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);
        }

(I have not tried this), but it seems that you can do this using the "elevate" program from here (我没试过这个),但似乎你可以使用这里的“提升”程序来做到这一点

also read this for UAC overview 也请阅读此内容以获取UAC概述

After 2 days of testing i found the following solution. 经过2天的测试,我发现了以下解决方案。

The error comes up when the Vista UAC functionality is activated. 启动Vista UAC功能时出现错误。 UAC shows a question dialog everytime, when a process needs administrator rights. 当进程需要管理员权限时,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 ProcessBuilder command 命令,我现在使用新的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 此处还描述了执行外部程序

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

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