3

Ok, I know this is probably a noobish question, but I'm pretty new to Java, and it'll probably be fairly easy to answer. What I'm trying to do is make a program that will use a file path to open Firefox, but it seems there is a problem with the file path. I did some research and used the double slash to nullify the escape characters, but it still doesn't work. I think it has to do with the fact that there spaces in some of the directories' names. Here is my code:

import java.io.IOException;

public class Automation {

public static void main(String[] args) throws IOException {
        Process p = Runtime.getRuntime().exec("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
    }
}

I know its pretty simple, but I can't still figure it out. Any help is appreciated.

user2399735
  • 27
  • 1
  • 1
  • 2

3 Answers3

3
Process p = Runtime.getRuntime().exec("\"C:\\Program Files (x86)\\Mozilla firefox\\firefox.exe\"");

... or with Java 7 against Windows ...

String[] command = new String[] {
    "C:" +
    File.separator + 
    "Program Files (x86)" +
    File.separator + 
    "Mozilla firefox" + 
    File.separator + 
    "firefox.exe"    
};
Process p = Runtime.getRuntime().exec(command);
Mena
  • 47,782
  • 11
  • 87
  • 106
  • 1
    When I do this, it says "Executable name has embedded quote, split the arguments" Not sure what that means. – user2399735 May 19 '13 at 20:51
  • @LakshithaRanasingha after a double-check, it doesn't seem to work with all Java versions. Namely, it might fail in Java 7. Try this post for more alternatives: http://stackoverflow.com/questions/2243993/how-to-execute-command-line-exe-file-in-java – Mena May 19 '13 at 21:31
  • @Mena - please update your answer with those findings, because that might help other readers. – Laksitha Ranasingha May 19 '13 at 21:40
  • Ok I've edited my post. Note that I'm neither on Windows nor on Java 7 so I can't _prove_ it works :( Feeling a bit lame now... – Mena May 19 '13 at 22:10
1
"\"C:\\ .......\""

So you can "escape" the blanks.

Maroun
  • 94,125
  • 30
  • 188
  • 241
xtraclass
  • 445
  • 3
  • 7
1

The ideal solution for your problem should be like this.

String [] cmds = new String [1];
cmds[0] = "C:\\Program Files (x86)\\Mozilla firefox\\firefox.exe";
Process p = Runtime.getRuntime().exec(cmds);

This is because Runtime.getRuntime().exec() actually doesn't execute the program as command line interpreter does. So you need to use a parameter array when you have white spaces in the path. you can provide extra flags/options in this array (ex: open).

This is some extra information. As far as I know Windows is perfectly happy with forward slashes (/), because Windows API accepts forward and backward slashes (starting from MS DOS 2.0 i think). for example you can do dir "c:/Program Files (x86)" will work fine give you the directory list. Furthermore, without white spaces Process p = Runtime.getRuntime().exec("C:/SomeProgram/someprogram.exe"); works fine . However the recommended way is to get the file separator from the environment. That's using File.separator

Laksitha Ranasingha
  • 4,321
  • 1
  • 28
  • 33