Getting java gui to open a webpage in a web browser

I am trying to get java gui to open a webpage. So gui runs some code that does something and then creates an html file. Then I want this file to open in a web browser (preferably Firefox) immediately after creating it. How should I do it?

+33
java html user-interface
Mar 02 '09 at 11:47
source share
4 answers

If you use Java 6 or higher, see the Desktop API, specifically browse . Use it like this (not tested):

// using this in real life, you'd probably want to check that the desktop // methods are supported using isDesktopSupported()... String htmlFilePath = "path/to/html/file.html"; // path to your new file File htmlFile = new File(htmlFilePath); // open the default web browser for the HTML page Desktop.getDesktop().browse(htmlFile.toURI()); // if a web browser is the default HTML handler, this might work too Desktop.getDesktop().open(htmlFile); 
+39
Mar 02 '09 at 11:54
source share

Ya, but if you want to open the webpage in your default web browser using a java program, you can try using this code.

 /// file OpenPageInDefaultBrowser.java public class OpenPageInDefaultBrowser { public static void main(String[] args) { try { //Set your page url in this string. For eg, I m using URL for Google Search engine String url = "http://www.google.com"; java.awt.Desktop.getDesktop().browse(java.net.URI.create(url)); } catch (java.io.IOException e) { System.out.println(e.getMessage()); } } } /// End of file 
+26
Sep 14 '10 at 8:56
source share

I know that all of these answers basically answered the question, but here is the code for a method that gracefully fails.

Please note that the line may be the location of the html file

 /** * If possible this method opens the default browser to the specified web page. * If not it notifies the user of webpage url so that they may access it * manually. * * @param url * - this can be in the form of a web address (http://www.mywebsite.com) * or a path to an html file or SVG image file etc */ public static void openInBrowser(String url) { try { URI uri = new URL(url).toURI(); Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null; if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) { desktop.browse(uri); } else { throw new Exception("Desktop not supported, cannout open browser automatically"); } } catch (Exception e) { /* * I know this is bad practice * but we don't want to do anything clever for a specific error */ e.printStackTrace(); // Copy URL to the clipboard so the user can paste it into their browser StringSelection stringSelection = new StringSelection(url); Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard(); clpbrd.setContents(stringSelection, null); // Notify the user of the failure WindowTools.informationWindow("This program just tried to open a webpage." + "\n" + "The URL has been copied to your clipboard, simply paste into your browser to access.", "Webpage: " + url); } } 
+2
Sep 07 '14 at 18:35
source share

I have used BrowserLauncher2 with success. It will call the default browser on all tested platforms. I use this to demonstrate software through JNLP. The software downloads, launches and controls the user's browser on information pages / feedback, etc.

JDK 1.4 and higher, I believe.

0
Mar 02 '09 at 12:58
source share



All Articles