0

I am trying to execute an exe file through Java code. I wrote below a simple code in Eclipse but got error. Tried multiple solutions but all in vain.

My code:

package com.runExeFile;

import java.io.File;

public class ClassA {

    public static void main(String[] args) throws Exception {
         Runtime.getRuntime().exec("C:\\FlashBuild\\14_09_2017_play_27_0_r0_137\\FF_32Release\\Something.exe");
    }

}

The Error I am getting:

Exception in thread "main" java.io.IOException: Cannot run program "C:\FlashBuild\14_09_2017_play_27_0_r0_137\FF_32Release\install_flash_player_27_plugin.exe": CreateProcess error=740, The requested operation requires elevation
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at com.runExeFile.ClassA.main(ClassA.java:9)
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>(Unknown Source)`enter code here`
    at java.lang.ProcessImpl.start(Unknown Source)
Cristian Ramon-Cortes
  • 1,838
  • 1
  • 19
  • 32
  • It's what the error states: You have to run the program with elevated permissions. – Jerrybibo Sep 15 '17 at 06:10
  • 1
    Possible duplicate of [CreateProcess error=740, The requested operation requires elevation](https://stackoverflow.com/questions/5853529/createprocess-error-740-the-requested-operation-requires-elevation) – Cristian Ramon-Cortes Sep 15 '17 at 06:18

2 Answers2

1

It is because you need to run the program as administrator. To run the program as administrator here is the code. Error 740 is because of that only. See these link

CreateProcess error=740, The requested operation requires elevation

Java: run as administrator

import java.io.IOException;

public class RunAsAdminExample {
    public static void main(String[] args) throws IOException {
        Process myappProcess = Runtime.getRuntime().exec("powershell.exe Start-Process notepad.exe -verb RunAs");
    }
}
Abhishek Honey
  • 645
  • 4
  • 13
  • Finally got success with below line of code but still i did not get how it works. Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+PathOfExe); – Rajesh Verma Sep 15 '17 at 06:30
0

I had done it recently. and the way I did was

try {
    File fileDirectory = new File("C:/someDirectory");
    Runtime.getRuntime().exec(new String[]{"cmd","/C","start someRunnable.exe"}, null, fileDirectory);
} catch (IOException e) {
    e.printStackTrace();
}

Which you needed to specify the directory to run and the start Command prompt to run the executable.

biligunb
  • 41
  • 8