I'm creating an application that with continuously attempt to run a list of programs until they are open. I'm having an issue when it comes to programs that require administrative priveleges however.

I've done some searching and found the below script (in createElevationScript()) that should run a program with administrative priveleges, however it doesn't start the program successfully.

Here's the relevant code:

    public void tryStart()
    {
        if (!checkRunning())
        {
            try
            {
                ProcessBuilder processBuilder = new ProcessBuilder();
                processBuilder.command(path);

                processBuilder.start();
            }
            catch (IOException e)
            {
                System.out.println("ForceStartUp: Insufficient privileges to run ProgramEntry with Name '" + name + "' at Path '" + path + "', attempting to start elevated");
                tryElevated();
            }
        }
    }

    public void tryElevated()
    {
        if (!checkRunning())
        {
            try
            {
                elevationScript = createElevationScript();
                String[] commandArray = new String[] {"cmd.exe", elevationScript.getAbsolutePath(), path};

                ProcessBuilder processBuilder = new ProcessBuilder();
                processBuilder.command(commandArray);

                processBuilder.start();

                elevationScript.delete();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    public File createElevationScript()
    {
        File elevatorScriptFile = new File(elevatorPath);

        if (elevatorScriptFile.exists())
            elevatorScriptFile.delete();

        try
        {
            elevatorScriptFile.createNewFile();
            elevatorScriptFile.deleteOnExit();

            FileWriter fw = new FileWriter(elevatorScriptFile, false);
            fw.write(
                    "@echo Set objShell = CreateObject(\"Shell.Application\") > %temp%\\sudo.tmp.vbs\r\n"
                            + "@echo args = Right(\"%*\", (Len(\"%*\") - Len(\"%1\"))) >> %temp%\\sudo.tmp.vbs\r\n"
                            + "@echo objShell.ShellExecute \"%1\", args, \"\", \"runas\" >> %temp%\\sudo.tmp.vbs\r\n"
                            + "@cscript %temp%\\sudo.tmp.vbs\r\n"
                            + "del /f %temp%\\sudo.tmp.vbs\r\n"
            );
            fw.flush();
        } catch (IOException ignored) {}

        return elevatorScriptFile;
    }

Any idea's on how to get this working?

Recommended Answers

All 5 Replies

Are you trying to create code that will run something with elevated privileges without the user needing to input an admin password?

The code should pop up a UAC window so the user can put in their passowrd if need be, otherwise it just runs with elevated priveleges

Linux or Windows?
Doesn't Windows throw up the authorisation wondow as required?
Linux: Have you tried a simple sudo - doesn't that pop up the passsword request automatically?

Windows, it should but it isn't. Throwing an IOException.

"java.io.IOException : "program.exe": CreateProcess error=740, The requested operation requires elevation". 

I figured out a solution using this tool which works, but isn't the best solution.

If anyone finds a better solution, that would be much appreciated!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.