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");
austinthemassive
  • 5,907
  • 6
  • 21
  • 25
  • have you tried rt.exec("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"); – Katona Jul 30 '13 at 20:00
  • I did try, and the machine seems to throw an error at C:/Program. It doesn't get past that. – austinthemassive Jul 30 '13 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"}); – Katona Jul 30 '13 at 22:07

3 Answers3

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:

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • 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? – austinthemassive Jul 30 '13 at 20:37
  • 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. – dic19 Jul 30 '13 at 21:08
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.

coding_idiot
  • 13,526
  • 10
  • 65
  • 116
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;