Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8081307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:41:26+00:00 2026-06-05T16:41:26+00:00

I’m trying to run an external executable, but apparently it needs elevation. The code

  • 0

I’m trying to run an external executable, but apparently it needs elevation. The code is this, modified from an example of using ProcessBuilder (hence the array with one argument) :

    public static void main(String[] args) throws IOException {
        File demo = new File("C:\\xyzwsdemo");
        if(!demo.exists()) demo.mkdirs();
        String[] command = {"C:\\fakepath\\bsdiff4.3-win32\\bspatch.exe"};
        ProcessBuilder pb = new ProcessBuilder( command );
        Process process = pb.start();
        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;
        System.out.printf("Output of running %s is:\n", Arrays.toString(command));
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        try {
            int exitValue = process.waitFor();
            System.out.println("\n\nExit Value is " + exitValue);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

It returns this when run:

Exception in thread "main" java.io.IOException: Cannot run program "C:\Users\Gilliane\Downloads\bsdiff4.3-win32\bspatch.exe": CreateProcess error=740, The requested operation requires elevation

I’ve done some browsing around, and know that in C#, you can request elevation by doing this, (as seen from this thread):

startInfo.Verb = "runas";

However, I don’t see anything like that with ProcessBuilder. Another method would be to have the Elevation Tools installed on the target system, and to invoke the “elevate” prompt with ProcessBuilder. However, I would rather not force the people who use my program to also install those elevation tools.

Is there another way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:41:28+00:00Added an answer on June 5, 2026 at 4:41 pm

    This can’t be done with ProcessBuilder, you will need to call Windows API.

    I’ve used JNA to achieve this with code similar to the following:

    Shell32X.java:

    import com.sun.jna.Native;
    import com.sun.jna.Pointer;
    import com.sun.jna.Structure;
    import com.sun.jna.WString;
    import com.sun.jna.platform.win32.Shell32;
    import com.sun.jna.platform.win32.WinDef.HINSTANCE;
    import com.sun.jna.platform.win32.WinDef.HWND;
    import com.sun.jna.platform.win32.WinNT.HANDLE;
    import com.sun.jna.platform.win32.WinReg.HKEY;
    import com.sun.jna.win32.W32APIOptions;
    
    public interface Shell32X extends Shell32
    {
        Shell32X INSTANCE = (Shell32X)Native.loadLibrary("shell32", Shell32X.class, W32APIOptions.UNICODE_OPTIONS);
    
        int SW_HIDE = 0;
        int SW_MAXIMIZE = 3;
        int SW_MINIMIZE = 6;
        int SW_RESTORE = 9;
        int SW_SHOW = 5;
        int SW_SHOWDEFAULT = 10;
        int SW_SHOWMAXIMIZED = 3;
        int SW_SHOWMINIMIZED = 2;
        int SW_SHOWMINNOACTIVE = 7;
        int SW_SHOWNA = 8;
        int SW_SHOWNOACTIVATE = 4;
        int SW_SHOWNORMAL = 1;
    
        /** File not found. */
        int SE_ERR_FNF = 2;
    
        /** Path not found. */
        int SE_ERR_PNF = 3;
    
        /** Access denied. */
        int SE_ERR_ACCESSDENIED = 5;
    
        /** Out of memory. */
        int SE_ERR_OOM = 8;
    
        /** DLL not found. */
        int SE_ERR_DLLNOTFOUND = 32;
    
        /** Cannot share an open file. */
        int SE_ERR_SHARE = 26;
    
    
    
        int SEE_MASK_NOCLOSEPROCESS = 0x00000040;
    
    
        int ShellExecute(int i, String lpVerb, String lpFile, String lpParameters, String lpDirectory, int nShow);
        boolean ShellExecuteEx(SHELLEXECUTEINFO lpExecInfo);
    
    
    
        public static class SHELLEXECUTEINFO extends Structure
        {
            /*
      DWORD     cbSize;
      ULONG     fMask;
      HWND      hwnd;
      LPCTSTR   lpVerb;
      LPCTSTR   lpFile;
      LPCTSTR   lpParameters;
      LPCTSTR   lpDirectory;
      int       nShow;
      HINSTANCE hInstApp;
      LPVOID    lpIDList;
      LPCTSTR   lpClass;
      HKEY      hkeyClass;
      DWORD     dwHotKey;
      union {
        HANDLE hIcon;
        HANDLE hMonitor;
      } DUMMYUNIONNAME;
      HANDLE    hProcess;
             */
    
            public int cbSize = size();
            public int fMask;
            public HWND hwnd;
            public WString lpVerb;
            public WString lpFile;
            public WString lpParameters;
            public WString lpDirectory;
            public int nShow;
            public HINSTANCE hInstApp;
            public Pointer lpIDList;
            public WString lpClass;
            public HKEY hKeyClass;
            public int dwHotKey;
    
            /*
             * Actually:
             * union {
             *  HANDLE hIcon;
             *  HANDLE hMonitor;
             * } DUMMYUNIONNAME;
             */
            public HANDLE hMonitor;
            public HANDLE hProcess;
    
            protected List getFieldOrder() {
                return Arrays.asList(new String[] {
                    "cbSize", "fMask", "hwnd", "lpVerb", "lpFile", "lpParameters",
                    "lpDirectory", "nShow", "hInstApp", "lpIDList", "lpClass",
                    "hKeyClass", "dwHotKey", "hMonitor", "hProcess",
                });
            }
        }
    
    }
    

    Elevator.java:

    package test;
    
    import test.Shell32X.SHELLEXECUTEINFO;
    
    import com.sun.jna.WString;
    import com.sun.jna.platform.win32.Kernel32;
    import com.sun.jna.platform.win32.Kernel32Util;
    
    public class Elevator
    {
        public static void main(String... args)
        {
            executeAsAdministrator("c:\\windows\\system32\\notepad.exe", "");
        }
    
        public static void executeAsAdministrator(String command, String args)
        {
            Shell32X.SHELLEXECUTEINFO execInfo = new Shell32X.SHELLEXECUTEINFO();
            execInfo.lpFile = new WString(command);
            if (args != null)
                execInfo.lpParameters = new WString(args);
            execInfo.nShow = Shell32X.SW_SHOWDEFAULT;
            execInfo.fMask = Shell32X.SEE_MASK_NOCLOSEPROCESS;
            execInfo.lpVerb = new WString("runas");
            boolean result = Shell32X.INSTANCE.ShellExecuteEx(execInfo);
    
            if (!result)
            {
                int lastError = Kernel32.INSTANCE.GetLastError();
                String errorMessage = Kernel32Util.formatMessageFromLastErrorCode(lastError);
                throw new RuntimeException("Error performing elevation: " + lastError + ": " + errorMessage + " (apperror=" + execInfo.hInstApp + ")");
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.