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.

1
  • Use a ProcessBuilder. Commented May 19, 2013 at 20:51

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

4 Comments

When I do this, it says "Executable name has embedded quote, split the arguments" Not sure what that means.
@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: stackoverflow.com/questions/2243993/…
@Mena - please update your answer with those findings, because that might help other readers.
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...
1
"\"C:\\ .......\""

So you can "escape" the blanks.

Comments

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

2 Comments

This is not a totally correct answer. You have to use " around the cmd string, like in my answer above.
Really ? have you run this ? my JVM doesn't tell it. there's no need of escaping as you told. this is real code which executes in java 6 & 7.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.