1

I am trying to execute another file using Runtime and Process

try
{
Runtime run =   Runtime.getRuntime();
Process pro =   run.exec("C:\\Users\\user\\Desktop\\file.exe");

}
catch(Exception a)
{
    a.printStackTrace();
}

I can enter this command in either run or cmd and am able to open the file but running it through my program it won't open. There are no errors, it just doesn't open.

jn025
  • 2,755
  • 6
  • 38
  • 73

4 Answers4

1

You must do

Process pro =   run.exec("C:\\Users\\user\\Desktop\\file.exe",null,"C:\\Users\\user\\Desktop\\");

Please see Run .exe file from Java from file location

Community
  • 1
  • 1
Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173
  • the 3rd actual parameter is a File which was demonstrated in that example you linked, i followed this and it still wont open. – jn025 Jun 25 '14 at 10:45
  • Process pro = run.exec("C:\\Users\\user\\Desktop\\file.exe",null,new File("C:\\Users\\user\\Desktop")); – jn025 Jun 25 '14 at 10:46
1

To better understand what is going on (and it is actually a requirement of the Process class), you need to redirect the input and error streams of your process - and using a ProcessBuilder is the recommended way to start processes:

public static void main(String[] args) throws Exception {
    ProcessBuilder pb = new ProcessBuilder("C:\\Users\\user\\Desktop\\file.exe");
    runProcess(pb)
}

private static void runProcess(ProcessBuilder pb) throws IOException {
    pb.redirectErrorStream(true);
    Process p = pb.start();
    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
}
assylias
  • 321,522
  • 82
  • 660
  • 783
  • not getting anything printed, could this indicate that it's not reading the file properly? – jn025 Jun 25 '14 at 11:07
  • @joe reading what file? What is `file.exe` supposed to do? What happens when you run `C:\Users\user\Desktop\file.exe` from the command line? – assylias Jun 25 '14 at 11:08
  • It starts a video game, launching from the command line works as it should. – jn025 Jun 25 '14 at 11:09
  • but i've tried multiple files. launching normal files like "notepad" works fine from the code – jn025 Jun 25 '14 at 11:10
  • just wont open my exe files from any location – jn025 Jun 25 '14 at 11:11
  • how do you run it from the command line? Are you in the `C:\Users\user\Desktop` folder then run `file.exe`? What happens if you go to `c:` and run `C:\Users\user\Desktop\file.exe`? – assylias Jun 25 '14 at 11:16
  • 1
    You can set the working directory with `pb.directory(new File("c:\\Users\\user\\Desktop");` before calling `pb.start();`. – assylias Jun 25 '14 at 11:19
  • Alright problem seemed to be in the files themselves and the way I was calling them. Thanks for the replies, all methods worked after I changed the files. – jn025 Jun 25 '14 at 11:27
1

Try this way:

  String []cmdarray = new String[4];
  cmdarray[0] = "cmd";
  cmdarray[1] = "/c";
  cmdarray[2] = "start";
  cmdarray[3] = "C:\\Users\\user\\Desktop\\file.exe";
  Runtime.getRuntime().exec(cmdarray);
Frank ZHENG
  • 109
  • 5
1

Try this one, create a batch file ,like start_file.bat. The content like this:

cd C:\Users\user\Desktop ----- Goto this directory
C:   ----- This line is very important
file.exe

Both the two approaches work well.

  Runtime r  = Runtime.getRuntime();
  String []cmdarray = new String[4];
  cmdarray[0] = "cmd";
  cmdarray[1] = "/c";
  cmdarray[2] = "start";
  cmdarray[3] = "C:/users/desktop/start_file.bat";
  r.exec(cmdarray);

And this one:

  r.exec("C:/users/desktop/start_file.bat");
  You can read the output from this new process.
Frank ZHENG
  • 109
  • 5
  • Perhaps , windows is aware of LOCATION where the program starts. There are so many assumptions on root path,such as environment variables ,where the os can find related dlls.. – Frank ZHENG Jun 25 '14 at 12:12