How do I launch a standard browser from a Java application?

how to open url using standard browser with Java?

I am currently using this code to open a specific url (locally saved html file), which works fine when running the application from my IDE (Eclipse), but after bundling the software, it no longer works.

    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 suggestions? Thank you very much!

+2


source to share


3 answers


but after linking the software, it doesn't work anymore.



You cannot find a URL that points to resources inside the JAR file. You need to extract the resource (just get it InputStream

with getResourceAsStream()

) and save it elsewhere ( as a temporary file ?) And then view it instead.

+2


source


Any idea why this isn't working? You can try the JDIC , it hasn't been updated in a while, but should do the trick.



0


source


Launching the Bare Bones browser is a good solution. It's very easy to use:

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

      

0


source







All Articles