3

I am developing an java application from which I have to run xyz_setup.exe installer. I tried the following code

String command = "C:\\xyz_setup.exe"; 
Runtime.getRuntime().exec(command);`

But it was throwing the following error

java.io.IOException: Cannot run program "C:\Users\NewtonApples\Downloads\idman614.exe": CreateProcess error=740, The requested operation requires elevation
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    at java.lang.Runtime.exec(Runtime.java:615)
    at java.lang.Runtime.exec(Runtime.java:448)
    at java.lang.Runtime.exec(Runtime.java:345)
    at upendra.OpenExternalApplication.main(OpenExternalApplication.java:19)
Caused by: java.io.IOException: CreateProcess error=740, The requested operation requires elevation
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:189)
    at java.lang.ProcessImpl.start(ProcessImpl.java:133)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1021)
    ... 4 more

Can any one suggest me how to do this?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    Possible duplicate of http://stackoverflow.com/questions/1385866/java-run-as-administrator – madhairsilence Jan 18 '13 at 05:38
  • @madhairsilence as Java developer, you don't always have control over executable (such as `java.exe`) which runs your code - often doing manifest etc is not an option. This is legitimate problem, and one possible solution to it is to run the program via `cmd`, which knows how to request privilege elevation. He's not asking to elevate current process - he only wants to run elevated setup, which is a different problem, so this is not a duplicate. – Cozzamara Jan 18 '13 at 05:50

1 Answers1

5

Java (or likely any other process which uses CreateProcess system call directly) is not good with executables requiring access elevation. You can get around that by executing your program via shell:

  String command = "C:\\setup.exe";
  Runtime.getRuntime().exec("cmd /c "+command);
Cozzamara
  • 1,318
  • 1
  • 14
  • 22
  • This is running perfectly but when the executable file is in following location it was showing an error. **Path: E:\\New folder\\Setup.exe** – Upendra Siripurapu Jan 18 '13 at 17:30
  • 1
    That's because you have space in folder name - enclose it in double quotes: `Runtime.getRuntime().exec("cmd /c \"" + command + "\"");` – Cozzamara Jan 18 '13 at 18:30
  • Thank you. It's working fine. Is it possible to trace the progress of the installation. That means after completing the Setup.exe installation I have to display 'Successfully Completed Installation' message through java. Regard's – Upendra Siripurapu Jan 19 '13 at 04:35
  • 1
    But of course. [Runtime.exec](http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html#exec%28java.lang.String%29) returns object of type [Process](http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Process.html). On that, you can do `exitValue()` to obtain exit code of your setup program. `getOutputStream()` and `getErrorStream()` will give you its standard output and standard error streams. To get all of that, you have to do `Process.waitFor()` first, of course, to wait for it to finish. Non-zero exit code most likely indicates failure, but check your specific setup. – Cozzamara Jan 19 '13 at 05:33