0

I am trying to open up a new tab in firefox (or just a new window) from my java program. I am transferring the code over from Ubuntu to Windows 7. I am doing something like this but it is throwing an exception.

Runtime rt = null;
...
rt =  Runtime.getRuntime();
...
rt.exec("C:\\Program Files\\Mozilla Firefox\\firefox.exe");

5 Answers5

3

You may want to take a look at the java.awt.Desktop.browse(URI uri) method. This opens the given uri in the default browser on the system and has the benefit that it will also work on non-Windows systems.

Jan B
  • 399
  • 3
  • 8
  • 1
    `browse` method is not static. To get object on which JVM can invoke it we can use something like `java.awt.Desktop.getDesktop().browse(new URI("www.google.com"));` – Pshemo Dec 31 '12 at 18:58
1

The folowing worked for me to open up firefox and a new tab for google.com

rt.exec("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe google.com");
John Dvorak
  • 26,799
  • 13
  • 69
  • 83
Kevin D
  • 3,564
  • 1
  • 21
  • 38
1

There is uniform way to open browser: (at least it works like a charm at my desktop)

// Start browser
if (Desktop.isDesktopSupported()) {  
    Desktop dt = Desktop.getDesktop();  
    if (dt.isSupported(Desktop.Action.BROWSE)) {  
        File f = new File(filePath);
        dt.browse(f.toURI());  
    }  
} 
Andrey
  • 853
  • 9
  • 27
0

For windows, you can try the following.

rt.exec("cmd /c C:/Program Files/Mozilla Firefox/firefox.exe");

or

String[] commands = {"cmd", "/c", "C:/Program Files/Mozilla Firefox/firefox.exe"};  
rt.exec(commands);
John Dvorak
  • 26,799
  • 13
  • 69
  • 83
muruga
  • 1,073
  • 8
  • 16
  • Thanks @JanDvorak, the escape is there. But in SO, it shows only one backslash though. – muruga Dec 31 '12 at 18:47
  • My problem was I didnt choose the x86 program files... Anyone know how to have it navigate to a page after opening? –  Dec 31 '12 at 18:49
  • @BumSkeeter you can refer to firefox.exe arguments. You can pass the URL as a parameter as well in the commands array. – muruga Dec 31 '12 at 18:50
  • @muruga didn't know Markdown applied `\\ `=>`\ `. Seems I have a new thing to learn :-) – John Dvorak Dec 31 '12 at 18:56
0

The solutions proposed above did not work with me (win 10), however a small manipulation solved my problem (add just start in front of /c

rt.exec("cmd /c start firefox");