Runtime.exec() with absolute directory

Lily picture Lily · Jul 9, 2009 · Viewed 9.5k times · Source

I would like to use Runtime.exec() to initiate another process in a directory with spaces. (It sounds stupid but I really want to satisfy my curiosity)

Details of the problem(simplified version) I have a myprogram.exe locates in C:/Program Files/MyProgram.

What I observe: 1). when I call Runtime.exec("C://Program Files//MyProgram//myprogram.exe"), IOException indicates: can't find file C:/Program, with error code = 2. I think it must a result of the space

2). I tried: Runtime.exec("/"C://Program Files//MyProgram//myprogram.exe/""), IOException indicates: can't find the file: "C:/Program Files/MyProgram/myprogram.exe"

3). I learnt my lesson and try an overloaded version of Runtime.exec(): File dir = new File("C://Program Files//MyProgram//"); And run: Runtime.exec("myprogram.exe", null, dir), and it says: can't find file myprogram.exe in C:/Program Files/MyProgram/

4). I tried 1), instead of "Program Files", I rename it to Program_Files, everything works

I know I can also use ProcessBuilder to do similar thing, but I can't let it go in my heart...(Maybe it's my weakness). Can anyone tell me why 2) and 3) does not work?

Thanks.

Answer

Lily picture Lily · Jul 9, 2009

I saw there was another answer several minutes ago, in the comment region, I finally find the way to solve it. I am sorry that I don't know how that answer was deleted... or who gave the answer by commenting the answer...

UPDATE: through some further investigation, I found out it's Matt Kane that gave the right answer by commenting. Please allow me to express my deep gratitude for your kind help. ;-)

Here is how I tackle it according to Matt's comment, and tested working:

String[] cmdArray = new String[]{"C://Program Files//MyProgram//myprogram.exe", arg1, arg2};
Runtime.exec(cmdArray);

where arg1 and arg2 are two parameters passing to myprogram.exe

Use the overloaded version of exec():

1)Process exec(String[] cmdarray)

2)Process exec(String[] cmdarray, String[] envp)

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

Either one will work. Here I would like to mention the difference between dir in 3) and the absolute path in cmdarray, which could be ""C://Program Files//MyProgram//" in my case.

In dir in 3), you can specify the directory you run the .exe, all relative directory you specify will be appended after this dir. Example: if your dir is C:/Hello World, and in your .exe you gonna store a file in /folder1, finally you will find the file locates in C:/Hello World/folder1

However, dir does not work for the executable.(In my case, it's myprogram.exe). For example, if your .exe locates in C:/Program Files/MyProgram/, and you have already set dir to C:/Program Files/MyProgram. You can't successfully run the program without specifying the absolute path of the executable file. In my case, you can only succeed through: cmdarray[0] = "C://Program Files//MyProgram//myprogram.exe" If you make it wrong, the error will look like this: java.io.exception: Cannot run program "myprogram.exe" (in directory "C:/Program Files/MyProgram/"): CreateProcess error=2, The system cannot find the file specified. Note that it only say, "can't run in ** directory" instead of saying "can't find file in *** directory". I thought it's quite ridiculous, but anyway, that's the way how it works.