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?
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:Elevator.java: