Open Browser in Java windows or Linux
Written on July 18, 2008 at 2:13 pm by
mkyong
Today i found a very useful java code, it can open browser from java application in windows or Linux.
package com.mkyong; public class StartBrowser { public static void main(String args[]) { String url = "http://www.google.com"; String os = System.getProperty("os.name").toLowerCase(); Runtime rt = Runtime.getRuntime(); try{ if (os.indexOf( "win" ) >= 0) { // this doesn't support showing urls in the form of "page.html#nameLink" rt.exec( "rundll32 url.dll,FileProtocolHandler " + url); } else if (os.indexOf( "mac" ) >= 0) { rt.exec( "open " + url); } else if (os.indexOf( "nix") >=0 || os.indexOf( "nux") >=0) { // Do a best guess on unix until we get a platform independent way // Build a list of browsers to try, in this order. String[] browsers = {"epiphany", "firefox", "mozilla", "konqueror", "netscape","opera","links","lynx"}; // Build a command string which looks like "browser1 "url" || browser2 "url" ||..." StringBuffer cmd = new StringBuffer(); for (int i=0; i<browsers.length; i++) cmd.append( (i==0 ? "" : " || " ) + browsers[i] +" \"" + url + "\" "); rt.exec(new String[] { "sh", "-c", cmd.toString() }); } else { return; } }catch (Exception e){ return; } return; } }
This article was posted in Java category.
Oracle Magazine - Free Magazine
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for Java's developers and DBAs, and more.
Securing & Optimizing Linux: The Hacking Solution - Free Guide
A comprehensive collection of Linux security products and explanations in the most simple and structured manner on how to safely and easily configure and run many popular Linux-based applications and services.
The Windows 7 Guide: From Newbies to Pros - Free Guide
In this 46 page guide you will be introduced to Windows 7 and what it has to offer. This guide will go over the software compatible issues, you will learn about the new taskbar, how to use and customize Windows Aero, what Windows 7 Libraries are all about, what software is included in Windows 7, and how easy networking is with Windows 7 along with other topics.
All Java Tutorials
- Java Core Technology - Java RegEx, Java XML, Java I/O, Java Misc
- J2EE Frameworks - Hibernate, Spring 2.5, Spring MVC, Struts 1.x, Struts 2.x
- Build Tools - Maven, Archiva
- Unit Test - jUnit, TestNG
- Client Scripts - jQuery
Superb site, where did you come up with the knowledge in this piece of writing? I’m pleased I found it though, ill be checking back soon to see what other articles you have.
Can it open in a new IE session rather than opening in existing opened session.
Thank you
Can be done MUCH easier: Desktop.getDesktop().browse(…) on ANY platform.
Thanks for this…
see http://java.sun.com/javase/6/docs/api/java/awt/Desktop.html
Hi guys, thanks Ryzhikov and Richard sharing this information.
That’s great!!!
Thanks a lot!!
Thanks
thanx, but why does it open in ie?? i want it to open in firefox..
It’s purposely because IE is your default internet browser