Bare Bones Browser
Launch for Java
Use Default Browser to Open a Web Page from a Swing Application
Java is often touted as the programing language of the Internet, so you would think Java might include a standard platform-independent mechanism to launch the user's default web browser. Unfortunately, this commonly needed feature is left to the application developer to build, and it's not particularly easy.Bare Bones
The Bare Bones Browser Launch solution is intended for those with simple requirements or those just looking for an educational "Hello, World" type tutorial on the subject. This 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. (Note: The release of Java 6 addresses this issue for those running exclusively on that version.)
Let's jump straight to the code:
BareBonesBrowserLaunch.java
///////////////////////////////////////////////////////// // Bare Bones Browser Launch // // Version 2.0 (May 26, 2009) // // By Dem Pilafian // // Supports: // // Mac OS X, GNU/Linux, Unix, Windows XP/Vista // // Example Usage: // // String url = "http://www.centerkey.com/"; // // BareBonesBrowserLaunch.openURL(url); // // Public Domain Software -- Free to Use as You Like // ///////////////////////////////////////////////////////// import java.lang.reflect.Method; import javax.swing.JOptionPane; import java.util.Arrays; public class BareBonesBrowserLaunch { static final String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "seamonkey", "galeon", "kazehakase", "mozilla", "netscape" }; public static void openURL(String url) { String osName = System.getProperty("os.name"); try { if (osName.startsWith("Mac OS")) { Class<?> fileMgr = Class.forName("com.apple.eio.FileManager"); Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] {String.class}); openURL.invoke(null, new Object[] {url}); } else if (osName.startsWith("Windows")) Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url); else { //assume Unix or Linux boolean found = false; for (String browser : browsers) if (!found) { found = Runtime.getRuntime().exec( new String[] {"which", browser}).waitFor() == 0; if (found) Runtime.getRuntime().exec(new String[] {browser, url}); } if (!found) throw new Exception(Arrays.toString(browsers)); } } catch (Exception e) { JOptionPane.showMessageDialog(null, "Error attempting to launch web browser\n" + e.toString()); } } }View/Download:
BareBonesBrowserLaunch.java
Launch the user's default browser from your Java Swing application with the following line of code:
BareBonesBrowserLaunch.openURL(urlStr);
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:
MyApp.java
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("http://www.centerkey.com "); 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); } }View/Download:
MyApp.java
Tutorial
Put both the "BareBonesBrowserLaunch.java" and "MyApp.java" 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.$ java MyApp
A window like the following will be displayed:

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.> "%JAVA_HOME\bin\javac" *.java
> java MyApp
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: BareBonesBrowserLaunch.jar, v2.0, 21KB] 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 "BareBonesBrowserLaunch.jar" →
Select "Javadoc location: (None)" →
Edit... →
Select "Javadoc in archive" →
Browse... to BareBonesBrowserLaunch.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
| Message: | |
|---|---|
| Name: | |
| E-Mail: | |
Powered by PERFECT |
Random
"Thanks for the code, it's really very simple and understandable, and usefull too...."
M., June 19, 2008
Great class, thanks!"
A.C., June 14, 2008
"Very, very useful, thank you!"
B., May 7, 2008
"Most awesome. I am using your idea along with httpUnit and portletUnit to allow me to see the generated code. This allows me to work with the generated html with out the long deploy process."
M.S., May 1, 2008
"PERFECTO!!!, ES JUSTO LO QUE ESTABA BUSCANDO!!! MUCHAS GRACIAS!!"
D.G., April 15, 2008
"You just saved my college career!!!!!"
B.J., April 3, 2008
"Thanks a million, the world needs more people like you."
B., April 1, 2008
"love you, :*** love this solution jojojoooo"
R., March 24, 2008
"excellent - simple and useful!"
J.D., March 7, 2008
"The above code ROCKS. I have always had a feeling how to do that, but your way of making it platform-independent is just awesome. Thanks and its very kind of you to put it up for free."
E., March 5, 2008
"Thank's a lot. It's beautiful class. It work's great."
G., February 15, 2008
"Thankyou very much... It's works excelente, I've used it to display my java aplication help that is in a HTML file."
D.P., February 5, 2008
"Great class!"
S.D.M., January 23, 2008
"Greatly Apreciated, it is an awesome starting point!"
F.B., January 14, 2008
"Thank you. you saved a lot of work to several developers. Thanks again."
F., September 14, 2007
"This is excellent and it works straight out of the tin: thanks so much for doing the work and for making it freely available. I'm using it in various GPL applications. Thank you so much!"
S.J., August 9, 2007
"Very Elegant Solution! Be Proud! ... and many thanks for sharing."
R.M., July 12, 2007
"You know, people like you make life a better place for me. It was really thoughtful of you to come up with this cute library that does a great deal. It works fast and nice. Thank you."
S.S., July 10, 2007
"Excellent. Too easy. Include the jar, add the BareBonesBrowserLaunch.openURL procedure and GO ON!! Average time = 10s Thanks a lot"
K.H., June 13, 2007
"I have been struggling to find a solution to open win32 since ages. You guys gave such a neat and easy solution. Could not believe it"
P.N., April 20, 2007
"tnx a lot. the codes helped me a lot."
V., April 17, 2007
"Thank's a lot : I've just replaced in my code the BrowserLauncher2 factory by your small and beautiful class. It work's great !"
E.H., April 15, 2007
"Just tried out your BareBonesBrowserLauncher. Thank you for a concise and helpful application."
K.M., April 2, 2007
"Very easy to use! Thanks, its exactly what i was looking for!"
F., March 31, 2007
"Thank you for the code"
P., March 20, 2007
"Perfect! Easy, simple and straight-forward. I like it! PowerFolder will use it in the future"
T.M., February 25, 2007
"thanks for giving such a nice code we were finding this things last 2 week for our project. thanks u very much to u and ur team"
D.M., February 24, 2007
"nice class! saved me a lot of time/work! go on!"
Z., February 5, 2007
"I had hoped that the JDIC library would answer the simple problem of launching a browser - but no... Bare Bones Browser Launch solution is *the* solution. Cheers!"
A., January 29, 2007
More comments...
M., June 19, 2008
Great class, thanks!"
A.C., June 14, 2008
"Very, very useful, thank you!"
B., May 7, 2008
"Most awesome. I am using your idea along with httpUnit and portletUnit to allow me to see the generated code. This allows me to work with the generated html with out the long deploy process."
M.S., May 1, 2008
"PERFECTO!!!, ES JUSTO LO QUE ESTABA BUSCANDO!!! MUCHAS GRACIAS!!"
D.G., April 15, 2008
"You just saved my college career!!!!!"
B.J., April 3, 2008
"Thanks a million, the world needs more people like you."
B., April 1, 2008
"love you, :*** love this solution jojojoooo"
R., March 24, 2008
"excellent - simple and useful!"
J.D., March 7, 2008
"The above code ROCKS. I have always had a feeling how to do that, but your way of making it platform-independent is just awesome. Thanks and its very kind of you to put it up for free."
E., March 5, 2008
"Thank's a lot. It's beautiful class. It work's great."
G., February 15, 2008
"Thankyou very much... It's works excelente, I've used it to display my java aplication help that is in a HTML file."
D.P., February 5, 2008
"Great class!"
S.D.M., January 23, 2008
"Greatly Apreciated, it is an awesome starting point!"
F.B., January 14, 2008
"Thank you. you saved a lot of work to several developers. Thanks again."
F., September 14, 2007
"This is excellent and it works straight out of the tin: thanks so much for doing the work and for making it freely available. I'm using it in various GPL applications. Thank you so much!"
S.J., August 9, 2007
"Very Elegant Solution! Be Proud! ... and many thanks for sharing."
R.M., July 12, 2007
"You know, people like you make life a better place for me. It was really thoughtful of you to come up with this cute library that does a great deal. It works fast and nice. Thank you."
S.S., July 10, 2007
"Excellent. Too easy. Include the jar, add the BareBonesBrowserLaunch.openURL procedure and GO ON!! Average time = 10s Thanks a lot"
K.H., June 13, 2007
"I have been struggling to find a solution to open win32 since ages. You guys gave such a neat and easy solution. Could not believe it"
P.N., April 20, 2007
"tnx a lot. the codes helped me a lot."
V., April 17, 2007
"Thank's a lot : I've just replaced in my code the BrowserLauncher2 factory by your small and beautiful class. It work's great !"
E.H., April 15, 2007
"Just tried out your BareBonesBrowserLauncher. Thank you for a concise and helpful application."
K.M., April 2, 2007
"Very easy to use! Thanks, its exactly what i was looking for!"
F., March 31, 2007
"Thank you for the code"
P., March 20, 2007
"Perfect! Easy, simple and straight-forward. I like it! PowerFolder will use it in the future"
T.M., February 25, 2007
"thanks for giving such a nice code we were finding this things last 2 week for our project. thanks u very much to u and ur team"
D.M., February 24, 2007
"nice class! saved me a lot of time/work! go on!"
Z., February 5, 2007
"I had hoped that the JDIC library would answer the simple problem of launching a browser - but no... Bare Bones Browser Launch solution is *the* solution. Cheers!"
A., January 29, 2007
More comments...





