Bare Bones Browser
Launch for Java

Use Default Browser to Open a Web Page from a Swing Application

Launch the user's default browser and open a web page from a Java application with the code below.

String url = ""; java.awt.Desktop.getDesktop().browse(;

The above code uses the Desktop library introduced in Java 6, so it will not work with Java 5.  However, the Bare Bones Browser Launch solution is designed to work on Java 5.  It will attempt to use the Desktop library from Java 6 but fall back to system calls if the library is unavailable.

Bare Bones

The Bare Bones Browser Launch solution is appropriate when a compact lightweight method to open a web page is needed.  Bare Bones is free and works on Mac OS X, GNU/Linux, Unix (Solaris), and Windows XP/Vista/7.

Let's jump straight to the code:

///////////////////////////////////////////////////////// // Bare Bones Browser Launch // // Version 3.2 (October 24, 2010) // // By Dem Pilafian // // Supports: // // Mac OS X, GNU/Linux, Unix, Windows XP/Vista/7 // // Example Usage: // // String url = ""; // // BareBonesBrowserLaunch.openURL(url); // // WTFPL -- Free to use as you like // ///////////////////////////////////////////////////////// import javax.swing.JOptionPane; import java.util.Arrays; public class BareBonesBrowserLaunch { static final String[] browsers = { "x-www-browser", "google-chrome", "firefox", "opera", "epiphany", "konqueror", "conkeror", "midori", "kazehakase", "mozilla" }; static final String errMsg = "Error attempting to launch web browser"; public static void openURL(String url) { try { //attempt to use Desktop library from JDK 1.6+ Class<?> d = Class.forName("java.awt.Desktop"); d.getDeclaredMethod("browse", new Class[] {}).invoke( d.getDeclaredMethod("getDesktop").invoke(null), new Object[] {}); //above code mimicks: java.awt.Desktop.getDesktop().browse() } catch (Exception ignore) { //library not available or failed String osName = System.getProperty(""); try { if (osName.startsWith("Mac OS")) { Class.forName("").getDeclaredMethod( "openURL", new Class[] {String.class}).invoke(null, new Object[] {url}); } else if (osName.startsWith("Windows")) Runtime.getRuntime().exec( "rundll32 url.dll,FileProtocolHandler " + url); else { //assume Unix or Linux String browser = null; for (String b : browsers) if (browser == null && Runtime.getRuntime().exec(new String[] {"which", b}).getInputStream().read() != -1) Runtime.getRuntime().exec(new String[] {browser = b, url}); if (browser == null) throw new Exception(Arrays.toString(browsers)); } } catch (Exception e) { JOptionPane.showMessageDialog(null, errMsg + "\n" + e.toString()); } } } }

Launch the user's default browser from your Java Swing application with the following line of code:
This is a fire and forget method -- no further communication or confirmation is provided.  However, a pop-up error message will be displayed to the user in most cases if a failure is encountered.

MyApp Test Program

You can try out the cross-platform Bare Bones Browser Launch with this small standalone test program:

import java.awt.event.*; import javax.swing.*; public class MyApp { public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(); final JTextField urlField = new JTextField(" "); JButton webButton = new JButton("Web Trip"); webButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { BareBonesBrowserLaunch.openURL(urlField.getText().trim()); } } ); frame.setTitle("Bare Bones Browser Launch"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel.add(new JLabel("URL:")); panel.add(urlField); panel.add(webButton); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } }


Put both the "" and "" files into a folder, and issue the following command line instructions:
$ javac *.java
$ java MyApp
As this test in run entirely from the current folder, there should be no package statement in either Java file.  The first command compiles the two Java files into class files, and the second command runs the test program.

A window like the following will be displayed:
Click the "Web Trip" button to launch the default browser.

Of course, you'll need the Java JDK for this work, and you may also need to specify the full path to the Java commands.  On Microsoft Windows for example, the above commands would become something like:
> for /d %%i in ("\Program Files\Java\jdk*") do set JAVA_HOME=%%i
> "%JAVA_HOME\bin\javac" *.java
> java MyApp
That's it.

JAR Library

Instead of putting the Bare Bones Browser Launch code directly in your project, you can alternatively use it as an external JAR library [file: bare-bones-browser-launch-3.1.jar, 25KB] complete with source code and Javadoc.  Tell your IDE, such as Eclipse or NetBeans, to include the JAR file into your project and then add the following "import" statement to the class responsible for opening a web page:
import com.centerkey.utils.BareBonesBrowserLaunch;
The code completion feature in most IDEs will automatically create the above "import" statement for you.

The steps for including an external JAR into your project will vary depending on your IDE.  In Eclipse, open your project and navigate through these menus and options (screenshot):
Project   →   Properties   →   Java Build Path   →   Libraries   →   Add External JARs...
Now link to the Javadoc included within the JAR (screenshot):
Expand "bare-bones-browser-launch-3.1.jar"   →   Select "Javadoc location: (None)"   →   Edit...   →   Select "Javadoc in archive"   →   Browse... to bare-bones-browser-launch-3.1.jar   →   2nd Browse... to doc
Finish the configuration process and you're ready to use the Bare Bones Browser Launch including the help activated with the F1 key.

Questions or Comments

Send us a message

Powered by PERFECT

All the fields are optional.  However, if you want a response, make sure to provide your email address.



Mac Java!
"That was AWESOME!!!  It was easy to use and it worked awesomely.  Thanks!"
S.M., January 1, 2010

"Thank you very much.  I used the code in DrJava Project and it works.  I gave you credit there"
S.Z., December 7., 2009

"thanks it's work for solving a very long url sending problem"
D., November 24, 2009

"Love your Launcher!  Thanks!"
R.K., October 28, 2009

"This class is very usefull.  thank you for sharing this dud"
O., October 4, 2009

"Thanks a lot code is very clear and easy.  It helps me a lot as my application users use Both windows and Linux."
R.I.K., September 17, 2009

"Thanks for the class...  lightweight and does exactly what it needs to!"
S.B., August 27, 2009

"Thanks man!  I got my RSS Feed in Java to work!"
H., July 9, 2009

"You folks are TOP NOTCH!  As a rather curmudgeon-ish IBM Midrange consultant who is teaching himself Java, it was a real treat to find code available that did EXACTLY what I wanted with NO MODS WHATSOEVER!  So many of the classes publicly available require extensive rework to be useful."
J., May 22, 2009

"Fantastic!  Small yet powerful!"
M., May 12, 2009

"Hi, thanks for this great solution.  I would like to share some enhancements: (1) try "xdg-open" and "sensible-browser" before calling browsers directly, and (2) first try the JDIC method and exec the browser only if that fails."
W., April 16, 2009

"Gut gemacht" ("Well done" — Google Translator)
A.S., February 10, 2009

"Thanks so much for this code...  This is exactly what I was looking for! ;)"
T., December 11, 2008

"Thank you much, Simple, fast, self explained...  Cheers !!!"
A., December 9, 2008

"Works perfect.  K.I.S.S. at its best.  Thank you"
K.B., December 4, 2008

"Excellent class.  Nice and small, and works like a charm on all main platforms!  Thanks so much for making it available!"
C., October 10, 2008

"Brilliant code, and instructions easy to understand!  Thank you."
D.B., October 6, 2008

"This is one of the most helpful things I have found on the net in a long long time.  Thank you."
J.L., September 12, 2008

"Great code.  Used it to display generated pdf from iText."
S.B., July 8, 2008

More comments...