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 1.5 (December 10, 2005) // // By Dem Pilafian // // Supports: Mac OS X, GNU/Linux, Unix, Windows XP // // 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 java.util.Arrays; import javax.swing.JOptionPane; public class BareBonesBrowserLaunch { private static final String errMsg = "Error attempting to launch web browser"; 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 String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" }; String browser = null; for (int count = 0; count < browsers.length && browser == null; count++) if (Runtime.getRuntime().exec( new String[] {"which", browsers[count]}).waitFor() == 0) browser = browsers[count]; if (browser == null) throw new Exception("Could not find web browser"); else Runtime.getRuntime().exec(new String[] {browser, url}); } } catch (Exception e) { JOptionPane.showMessageDialog(null, errMsg + ":\n" + e.getLocalizedMessage()); } } }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); } }Tutorial
Put both the "BareBonesBrowserLaunch.java" and "MyApp.java" files into a folder, and issue the following command line instructions:
$ javac *.java
$ java MyApp
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 Windows for example, the "javac" command above would become something like:
> "\Program Files\Java\jdk1.6.0_02\bin\javac" *.java
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: BareBonesBrowserLaunch.jar, v1.5, 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
All the fields are optional. However, if you want a response, make sure to provide your e-mail address.
Random
"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
"This is the best code I was able to find for this type of application. I would have never gotten it to work without you. Thanks for sharing!!!"
K.W., December 22, 2006
"Thanks for the Bare Bones Browser Launch info! I would not have figured it out for myself."
B.M., December 7, 2006
"Thanks a lot for your product. I used it and worked right away... Saved me a lot of time... very funny, it seems, to work with you !!"
S.L., November 30, 2006
"Thank you so much for developing this class. It has saved me a lot of time and effort. I don't think I would have figured this one out by myself. Thanks a million it works great!!!"
A.G., November 15, 2006
"Thank you very much! This is terrific."
P.A., November 9, 2006
"Hi, very useful piece of code. In windows, it launches most docs, and exe's not just web pages. Saved us from messing with JDIC in our (Cande) application."
R., October 26, 2006
"I can't express how happy you just made me. Couldn't have worked easier and works like a charm. Thanks and more thanks"
M.E., October 16, 2006
"This is a very useful code snippet. Thanks for your effort."
S.E., October 11, 2006
"Great great great! Your java class solve my problem! :) I will try under linux and macos. At the moment it works fine under windows. Thank you very much!!"
S.B., September 25, 2006
"Thanks for the code. Very useful and valuable! Opening a html url, a pdf document is a snap."
E.B., September 20, 2006
"You just set a rather high standard for a website that gives a java developer free toys. Shootz, now we're all going to have to do it this way!"
K.O., August 30, 2006
"works perfect, thank you so much."
J., August 10, 2006
"This is simple and efficient. You saved me much tedious effort. Thanks!"
D.L.R., August 1, 2006
"Excellent travail !!! Merci beaucoup / Thanks a lot"
C., July 27, 2006
"You saved me a lot of time. If you're ever in NYC, let me know and I'll buy you a beer."
J.K., June 22, 2006
"Wow, this is so easy to use (worked on the first try!), and it works perfectly. I don't have much experience importing jars, but your instructions are fantastic. Thanks so much. (Now why can't Sun do this?)"
E., June 8, 2006
"Just what I needed, simple and elegant! That's the true power of open-source unleashed. Thank You Dem!"
D.J.S., May 28, 2006
"great job man! 10x"
E., May 18, 2006
"Nice piece of work. I am using the BareBonesBrowserLauncher.jar in my one of my programs (TDMiner on SourceForge.net)"
D., April 7, 2006
"Exactly what I needed, you guy's are great, thanks for helping me meet my deadlines!!"
L.P., March 21, 2006
"Thanks for sharing this very useful source code."
P.L.A., March 20, 2006
"REALLY COOL, thats what i needed"
D., February 26, 2006
"Thank you very much for your donation! Your code has solved a great deal of my problem!"
M., February 24, 2006
"Just what I was looking for!!"
E.R., February 15, 2006
"Sweet! Thank you."
E., January 29, 2006
"Thanks for sharing this nice code in the Public Domain."
R.R., January 24, 2006
"Very very good. Thanks, I appreciate your sharing of code"
Y., January 9, 2006
"very neat !!"
J.Q., December 23, 2005
"I liked your code very much. It'll probably save me a lot of time. I inted to use it to open a PDF file - in Windows, it works like a charm (all I did was to use 'file:///S:/test.pdf' as the URL), it also works for other file types (txt, doc, etc)."
J.C.S., December 22, 2005
"This is very helpful"
R., December 21, 2005
"Thanx! i will add a link to your page in my about window!"
G., December 6, 2005
"I've just spent several days trying to get a Java browser to work with my application. This code is the next best thing and does the job perfectly and, most importantly for me, easily!"
D.D., December 5, 2005
"Thank you for your generosity!"
A.P., December 3, 2005
"Thanx a lot! It works fine with WinXP!"
M.B., November 20, 2005
"Thank you for putting the BareBones Browser technique online. I'm using it! My personal open source site is here: www.RavenTools.com. Feel free to plunder it!!!!"
C.F. (the Raven), November 11, 2005
"Just wanna say thanx a million for releasing this code. Sun really should of created a built in method for it but thankfully the better people in this world such as yourselves are doing it for us!"
B., October 2, 2005
"You'd think that Sun could implement something like this using the default browser for whatever OS. Not only 'could' but 'must with threat of torture!'. :) The ole' JavaWorld BrowserControl don't work under MacOS. Yours does! Thank you very much!!!"
R.T., September 1, 2005
"Hey - I was trying to find out how to launch a browser in my code and found your site. I just added your BareBonesBrowserLaunch and it seems to work well - thank you very much."
D.Z., August 25, 2005
"This is a great resource !!!"
R., July 29, 2005
"I was basically going to reinvent the wheel until I found your code. Thanks so much for making it available!! It works very well!!"
W.S., July 26, 2005
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
"This is the best code I was able to find for this type of application. I would have never gotten it to work without you. Thanks for sharing!!!"
K.W., December 22, 2006
"Thanks for the Bare Bones Browser Launch info! I would not have figured it out for myself."
B.M., December 7, 2006
"Thanks a lot for your product. I used it and worked right away... Saved me a lot of time... very funny, it seems, to work with you !!"
S.L., November 30, 2006
"Thank you so much for developing this class. It has saved me a lot of time and effort. I don't think I would have figured this one out by myself. Thanks a million it works great!!!"
A.G., November 15, 2006
"Thank you very much! This is terrific."
P.A., November 9, 2006
"Hi, very useful piece of code. In windows, it launches most docs, and exe's not just web pages. Saved us from messing with JDIC in our (Cande) application."
R., October 26, 2006
"I can't express how happy you just made me. Couldn't have worked easier and works like a charm. Thanks and more thanks"
M.E., October 16, 2006
"This is a very useful code snippet. Thanks for your effort."
S.E., October 11, 2006
"Great great great! Your java class solve my problem! :) I will try under linux and macos. At the moment it works fine under windows. Thank you very much!!"
S.B., September 25, 2006
"Thanks for the code. Very useful and valuable! Opening a html url, a pdf document is a snap."
E.B., September 20, 2006
"You just set a rather high standard for a website that gives a java developer free toys. Shootz, now we're all going to have to do it this way!"
K.O., August 30, 2006
"works perfect, thank you so much."
J., August 10, 2006
"This is simple and efficient. You saved me much tedious effort. Thanks!"
D.L.R., August 1, 2006
"Excellent travail !!! Merci beaucoup / Thanks a lot"
C., July 27, 2006
"You saved me a lot of time. If you're ever in NYC, let me know and I'll buy you a beer."
J.K., June 22, 2006
"Wow, this is so easy to use (worked on the first try!), and it works perfectly. I don't have much experience importing jars, but your instructions are fantastic. Thanks so much. (Now why can't Sun do this?)"
E., June 8, 2006
"Just what I needed, simple and elegant! That's the true power of open-source unleashed. Thank You Dem!"
D.J.S., May 28, 2006
"great job man! 10x"
E., May 18, 2006
"Nice piece of work. I am using the BareBonesBrowserLauncher.jar in my one of my programs (TDMiner on SourceForge.net)"
D., April 7, 2006
"Exactly what I needed, you guy's are great, thanks for helping me meet my deadlines!!"
L.P., March 21, 2006
"Thanks for sharing this very useful source code."
P.L.A., March 20, 2006
"REALLY COOL, thats what i needed"
D., February 26, 2006
"Thank you very much for your donation! Your code has solved a great deal of my problem!"
M., February 24, 2006
"Just what I was looking for!!"
E.R., February 15, 2006
"Sweet! Thank you."
E., January 29, 2006
"Thanks for sharing this nice code in the Public Domain."
R.R., January 24, 2006
"Very very good. Thanks, I appreciate your sharing of code"
Y., January 9, 2006
"very neat !!"
J.Q., December 23, 2005
"I liked your code very much. It'll probably save me a lot of time. I inted to use it to open a PDF file - in Windows, it works like a charm (all I did was to use 'file:///S:/test.pdf' as the URL), it also works for other file types (txt, doc, etc)."
J.C.S., December 22, 2005
"This is very helpful"
R., December 21, 2005
"Thanx! i will add a link to your page in my about window!"
G., December 6, 2005
"I've just spent several days trying to get a Java browser to work with my application. This code is the next best thing and does the job perfectly and, most importantly for me, easily!"
D.D., December 5, 2005
"Thank you for your generosity!"
A.P., December 3, 2005
"Thanx a lot! It works fine with WinXP!"
M.B., November 20, 2005
"Thank you for putting the BareBones Browser technique online. I'm using it! My personal open source site is here: www.RavenTools.com. Feel free to plunder it!!!!"
C.F. (the Raven), November 11, 2005
"Just wanna say thanx a million for releasing this code. Sun really should of created a built in method for it but thankfully the better people in this world such as yourselves are doing it for us!"
B., October 2, 2005
"You'd think that Sun could implement something like this using the default browser for whatever OS. Not only 'could' but 'must with threat of torture!'. :) The ole' JavaWorld BrowserControl don't work under MacOS. Yours does! Thank you very much!!!"
R.T., September 1, 2005
"Hey - I was trying to find out how to launch a browser in my code and found your site. I just added your BareBonesBrowserLaunch and it seems to work well - thank you very much."
D.Z., August 25, 2005
"This is a great resource !!!"
R., July 29, 2005
"I was basically going to reinvent the wheel until I found your code. Thanks so much for making it available!! It works very well!!"
W.S., July 26, 2005

