0

So the following opens a new browser window when I put it in cmd manually:

cd C:/Program Files (x86)/Google/Chrome/Application&chrome.exe

However, when I tried to do this via a java program (see below), the command prompt opens and goes to the correct directory, but no new window opens. Any ideas of what I need to change?

    Runtime rt = Runtime.getRuntime();
    rt.exec("cmd.exe /c start cd C:/Program Files (x86)/Google/Chrome/Application&chrome.exe");
3
  • have you tried rt.exec("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"); Commented Jul 30, 2013 at 20:00
  • I did try, and the machine seems to throw an error at C:/Program. It doesn't get past that. Commented Jul 30, 2013 at 20:36
  • it seems Runtime.exec(String) tokenizes the string, but Runtime.exec(String[]) not, so my last guess would be rt.exec(new String[] {"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"}); Commented Jul 30, 2013 at 22:07

3 Answers 3

2

Try ProcessBuilderinstead of Runtime:

String command = "C:/Program Files (x86)/Google/Chrome/Application&chrome.exe";
ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", command);
Process p = pb.start();

See also:

Sign up to request clarification or add additional context in comments.

2 Comments

I'll give this a shot. What is the difference between Runtime and ProcessBuilder and, in your opinion, when should I use one over the other?
Honestly I don't know exactly what the difference is but I use ProcessBuilder mainly because you can set environment variables using pb.environment().put(key,value). Also it's more simple to run a command with many parameters separately instead to write them in one large string.
1

rt.exec("cmd.exe /c start cd \"C:/Program Files (x86)/Google/Chrome/Application&chrome.exe\"");

Not tested but this shall work, I just put the complete path in double quotes so that because of spaces it's not considered to be the next argument.

If that doesn't work, I suggest trying Apache Commons Exec library, because I always use that.

Here is some sample code from one of my applications :

CommandLine cmdLine = new CommandLine("cmd.exe");
                    cmdLine.addArgument("/c");
                    cmdLine.addArgument(".\\phantomjs\\nk\\batchbin\\casperjs.bat");
                    cmdLine.addArgument(".\\phantomjs\\nk\\batchbin\\dd.js");
                    cmdLine.addArgument(url);
                    cmdLine.addArgument(">" + rand);
                    DefaultExecutor executor = new DefaultExecutor();
                    int exitValue = executor.execute(cmdLine);

Using something like above the complete path to chrome.exe should be added as a new argument, and then the library will take care of escaping.

1 Comment

I actually just solved this using the Apache Commons library (recommended by another source). Thanks for the tip!
0

I run a chrome process using:

            ProcessBuilder builder = new ProcessBuilder();            
            builder.command("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", "https://your url");            
            Process process = builder.start();
            int exitCode = process.waitFor();        
            assert exitCode == 0;      

Comments

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.