I am trying to get a java gui to open a web page. So the gui runs some code that does things and then produces a html file. I then want this file to open in a web browser (preferrably Firefox) as soon as it is created. How would I go about doing that?
Asked
Active
Viewed 5.6k times
4 Answers
39
If you're using Java 6 or above, see the Desktop API, in particular 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);

Dave Jarvis
- 30,436
- 41
- 178
- 315

Dan Vinton
- 26,401
- 9
- 37
- 79
-
Hey. Still can't get it to work. I get the following error: "Error message: The system cannot find the path specified." This is even though I have given it the exact path. – The Special One Mar 03 '09 at 12:26
-
@TheSpecialOne - are you supplying a _local_ path to the file? eg: on a windows box, htmlFilePath would look like "c:\path\to\file.html". Don't use the URL syntax... – Dan Vinton Mar 04 '09 at 11:34
27
Ya, But if you want to open the webpage in your default web browser by a java program then 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

Ankur
- 1,268
- 18
- 22
3
I know that all of these answers have basically answered the question, but here is a the code for a method that fails gracefully.
Note that the string can be the location of an html file
/**
* If possible this method opens the default browser to the specified web page.
* If not it notifies the user of webpage's 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 e.t.c
*/
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);
}
}

Troyseph
- 4,960
- 3
- 38
- 61
0
I've used BrowserLauncher2 with success. It'll invoke the default browser on all platforms tested. I use this for demoing software via JNLP. The software downloads, runs and drives the user's browser to information pages/feedback etc.
JDK 1.4 and above, I believe.

Brian Agnew
- 268,207
- 37
- 334
- 440
-
If the URL of page get changed because of user activity can get that new URL using BrowserLauncher2? – Nikolay Kuznetsov Oct 10 '12 at 12:32