简体   繁体   中英

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.

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.

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

After 2 days of testing i found the following solution.

The error comes up when the Vista UAC functionality is activated. UAC shows a question dialog everytime, when a process needs administrator rights.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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