user1311286
user1311286

Reputation:

Java, Windows, open firefox

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");

Upvotes: 0

Views: 8516

Answers (5)

Thierno Abdoulaye
Thierno Abdoulaye

Reputation: 1

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");

Upvotes: 0

Andrey
Andrey

Reputation: 925

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());  
    }  
} 

Upvotes: 1

muruga
muruga

Reputation: 1073

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);

Upvotes: 0

Kevin D
Kevin D

Reputation: 3594

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");

Upvotes: 1

Jan B
Jan B

Reputation: 399

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.

Upvotes: 3

Related Questions