Main Tutorials

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 Author

author image
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

Subscribe
Notify of
35 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Talha
1 year ago

Is my ipadress is connected with other phone

Sanjay Kumar
2 years ago

I am facing an issue. If anybody facing the same please suggest the solution.

I am using the below code to open the URL in default browser :

Runtime runtime = Runtime.getRuntime();
runtime.exec(“rundll32 url.dll,FileProtocolHandler ” + primaryLink);

//Here primaryLink variable is holding the URL which i want to open into the browser.

This code is working fine on some machine and successfully open the URL in default browser.
But in some machine i have received the below exception while URL is invoking

Exception : java.io.IOException: Cannot run program “”/Program”: CreateProcess error=2, The system cannot find the file specified

Please help me here what action should i taken on these machine. So that problem is resolve. Please suggest.

Manish
3 years ago

How to restrict launch of browser if default browser is edge.

Manish
3 years ago

How to restrict launch of browser of default browser is edge.

Nikhil
3 years ago

Thank you !!

Parag
4 years ago

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?¿?

Frederick
4 years ago

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.

Vivek Mishra
5 years ago

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

Naveen Dennis
8 years ago

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

bpham
8 years ago

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

Charles
9 years ago

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

planty182
9 years ago
GuestFromPune
9 years ago

How to open url having sections e.g.,

“page.html#nameLink”

fly.kb
10 years ago

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

loke
10 years ago

Thanks

vasilis
10 years ago

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

chayabid
11 years ago

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

Cory B
11 years ago

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

Vikas Khandelwal
11 years ago

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..!!!

James
11 years ago

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?

Tapan Thaker
11 years ago

Thanks a ton , helped me a lot 🙂

Daniel Knudsen
12 years ago

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

chayabid
11 years ago
Reply to  Daniel Knudsen

Hi Daniel,

Do one thing.. if you know what to search, go to google, enter the word, hit search. Now copy the url its showing currently, and put in the code like

 

//String url = "http://www.google.com"; //comment this line
// I have searched for word pic, u can choose your word.
String url="http://www.google.co.in/search?hl=en-IN&source=hp&q=pic&gbv=2&oq=pic&gs_l=heirloom-hp.3..0l10.2906.3547.0.5250.3.3.0.0.0.0.234.656.2-3.3.0...0.0...1c.1.8R_O73gqzd0";

Well, but if you have a dynamic word, more expert comments is required… 🙂

phex
12 years ago

is this all i want hehe thanks again

Marvin Heidgerken
13 years ago

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.

krishna
14 years ago

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

Miguel
15 years ago

Thank you

Eugene Ryzhikov
15 years ago

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

Richard
15 years ago
jun
15 years ago

That’s great!!!

Tom4to
15 years ago

Thanks a lot!! 🙂

rammohan
15 years ago

Thanks

jlearner
14 years ago
Reply to  rammohan

thanx, but why does it open in ie?? i want it to open in firefox..