Peter
Peter

Reputation: 627

How to launch standard browser out of Java application?

how to I open a URL with the systems standard browser with Java?

I currently use this code for opening a specific URL (locally stored html file), which works fine when I run the application with my IDE (Eclipse), but after bundling the software, it doesn't work any more.

    url = MainWindow.class.getResource("mySite.html");

    helpMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {    
            try {
                java.awt.Desktop.getDesktop().browse(url.toURI());
            }
            catch (URISyntaxException e1) {
                e1.printStackTrace();
            }
            catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    });

Any suggestsions? Thank you very much!

Upvotes: 2

Views: 927

Answers (3)

Michael
Michael

Reputation: 35351

Bare Bones Browser Launch is a good solution. It's very easy to use:

BareBonesBrowserLaunch.openURL("http://www.stackoverflow.com");

Upvotes: 0

BalusC
BalusC

Reputation: 1109665

but after bundling the software, it doesn't work any more.

You cannot browse to URL's which points to resources inside a JAR file. You need to extract the resource (just get InputStream using getResourceAsStream()) and store it somewhere else (as temp file?) and then browse it instead.

Upvotes: 2

Guillaume
Guillaume

Reputation: 14671

Any idea why it doesn't work? You can try JDIC, it has not been updated for a while but should do the trick.

Upvotes: 0

Related Questions