views:

121

answers:

1

I wish to use the method with the following signature:

exec(String command, String[] envp, File dir)

Suppose my program is called "myprog.exe" and it is located at "C:\Program Files\My Software". What is the correct syntax for using Runtime.exec? I keep getting an error message "The system cannot find the file specified". To clarify I wish to start myprog.exe from the directory "C:\Program Files\My Software" and not from where the java program is running

+2  A: 

I would recommend using the other flavour of exec() instead:

exec(String[] cmdarray, String[] envp, File dir)

Using this method, you can pass the full path to the executable in cmdarray[0] and the command arguments (if any) in subsequent array elements. This is more robust than dealing with the quoting or escaping or whatever you might have to do to make it work with the simplistic exec().

To answer the other part of your question, be sure to pass the path where you want to start your program ("C:\\Program Files\\My Software") in the dir parameter of the above exec() call. Also note that I used \\ in the pathname because Java uses \ as an escape character, meaning you must use two in a literal string representing a pathname.

Greg Hewgill