Java Desktop library
Before Java 6 there was no built-in way to launch the user's default browers.
The above code uses the Desktop library introduced in Java 6, so it will not work with Java 5. 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.
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:
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 Swing program:
Instead of putting the Bare Bones Browser Launch code directly in your project, you can alternatively use it as an external JAR library ( bare-bones-browser-launch-v3.2.jar) 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:
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 the menus to the add JAR feature:
Now link to the Javadoc included within the JAR:
Finish the configuration process and you're ready to use the Bare Bones Browser Launch including the help activated with the F1 key.
"Thank you for perfect script! :) Very need for this at AIX 4.3!" A.B., July 24, 2013
"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
"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"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 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)." 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