Open Browser in Java windows or Linux

A very useful Java code, to open a web 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;		
  
}

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
25 Comment threads
5 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
28 Comment authors
ParagFrederickVivek MishraNaveen Dennisbpham Recent comment authors
newest oldest most voted
rammohan
Guest
rammohan

Thanks

Tom4to
Guest
Tom4to

Thanks a lot!! 🙂

jun
Guest
jun

That’s great!!!

Eugene Ryzhikov
Guest
Eugene Ryzhikov

Can be done MUCH easier: Desktop.getDesktop().browse(…) on ANY platform.

Miguel
Guest
Miguel

Thank you

krishna
Guest
krishna

Can it open in a new IE session rather than opening in existing opened session.

Marvin Heidgerken
Guest
Marvin Heidgerken

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.

phex
Guest
phex

is this all i want hehe thanks again

Daniel Knudsen
Guest
Daniel Knudsen

I got i opened 🙂
But does anybody know how put a text into the browser?
Example in Google, how to search on something directly?

//Daniel

Tapan Thaker
Guest
Tapan Thaker

Thanks a ton , helped me a lot 🙂

James
Guest
James

I have a videogame (its a socail game for a lot of people to play at one time together) and when i try to run this method, as if it were a link, it only opens the link/website on my computer. Why is this?

Vikas Khandelwal
Guest
Vikas Khandelwal

I got it open but if i want to insert one button or a link there also then what should I do for this ??
Thanks in advance..!!!

Cory B
Guest
Cory B

Of course the Desktop.getDesktop approach is much more brief, however Runtime.getRuntime().exec() has another issue – you have to consume the resulting input and error streams, otherwise you will find that resources are left open and you will eventually get errors that will appear almost unrelated, such as “too many open files”, etc.

See this link (old but still relevant) to see how….
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4

chayabid
Guest
chayabid

hi.. is it possible to hide the menubar/toolbar in the opening window? would be of much help.

vasilis
Guest
vasilis

Thank you my friend! That code was right and helpfull 🙂 Added you on facebook! Have a good night !

loke
Guest
loke

Thanks

fly.kb
Guest
fly.kb

Thanks Nice article. But can I open a URL which needs client certificate authentication in a new browser window. My certificate is in p12 format stored on client side

GuestFromPune
Guest
GuestFromPune

How to open url having sections e.g.,

“page.html#nameLink”

planty182
Guest
planty182
Charles
Guest
Charles

Works very well!! The other solution : Desktop.browse doesnt seem to work for me.

bpham
Guest
bpham

on Window OS, How to launch url from multiple browser (ie, chrome, firefox).?

Naveen Dennis
Guest
Naveen Dennis

How can we open a file (like a pdf) in a web-browser using Java?

Vivek Mishra
Guest
Vivek Mishra

Thanks
This worked for me
Runtime rt = Runtime.getRuntime();
rt.exec( “rundll32 url.dll,FileProtocolHandler ” + “http://localhost:8080”);

Frederick
Guest
Frederick

This was quite helpful. For various reasons I wanted to accomplish this task in JRE 1.4 and your method worked quite well. Thank you for posting this.

Parag
Guest
Parag

Hello
Thanks for your help

One query
Can we close same url only from browser tab after all works done of that webpage?¿?

Please can you help me?¿?