How to open default web browser using java

Can someone point me in the right direction how to open the default web browser and set the page to “www.example.com” thanks

+80
java
Mar 07 2018-11-23T00:
source share
7 answers

java.awt.Desktop is the class you are looking for.

 import java.awt.Desktop; import java.net.URI; // ... if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { Desktop.getDesktop().browse(new URI("http://www.example.com")); } 
+123
Mar 07 2018-11-11T00:
source share

Here is my code. It will open the given url in the default browser (cross-platform solution).

 import java.awt.Desktop; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; public class Browser { public static void main(String[] args) { String url = "http://www.google.com"; if(Desktop.isDesktopSupported()){ Desktop desktop = Desktop.getDesktop(); try { desktop.browse(new URI(url)); } catch (IOException | URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else{ Runtime runtime = Runtime.getRuntime(); try { runtime.exec("xdg-open " + url); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } 
+33
Aug 29 '13 at 11:12
source share

For me, the solution with Desktop.isDesktopSupported () does not work (windows 7 and ubuntu). Try this to open a browser from java code:

Windows:

 Runtime rt = Runtime.getRuntime(); String url = "http://stackoverflow.com"; rt.exec("rundll32 url.dll,FileProtocolHandler " + url); 

Mac

 Runtime rt = Runtime.getRuntime(); String url = "http://stackoverflow.com"; rt.exec("open " + url); 

Linux:

 Runtime rt = Runtime.getRuntime(); String url = "http://stackoverflow.com"; String[] browsers = { "epiphany", "firefox", "mozilla", "konqueror", "netscape", "opera", "links", "lynx" }; StringBuffer cmd = new StringBuffer(); for (int i = 0; i < browsers.length; i++) if(i == 0) cmd.append(String.format( "%s \"%s\"", browsers[i], url); else cmd.append(String.format(" || %s \"%s\"", browsers[i], url); // If the first didn't work, try the next browser and so on rt.exec(new String[] { "sh", "-c", cmd.toString() }); 

If you want to have a multi-platform application, you need to add an operating system check (for example):

 String os = System.getProperty("os.name").toLowerCase(); 

Window:

 os.indexOf("win") >= 0 

Mac:

 os.indexOf("mac") >= 0 

Linux:

 os.indexOf("nix") >=0 || os.indexOf("nux") >=0 
+26
Mar 02 '15 at 10:07
source share

You can also use Runtime to create a cross-platform solution:

 import java.awt.Desktop; import java.net.URI; public class App { public static void main(String[] args) throws Exception { String url = "http://stackoverflow.com"; if (Desktop.isDesktopSupported()) { // Windows Desktop.getDesktop().browse(new URI(url)); } else { // Ubuntu Runtime runtime = Runtime.getRuntime(); runtime.exec("/usr/bin/firefox -new-window " + url); } } } 
+6
Jul 30 '13 at 14:43
source share

As noted in Tim Cooper's answer, java.awt.Desktop provided this feature since Java 6 (1.6), but with the following warning:

Use the isDesktopSupported () method to determine if the Desktop API is available. On the Solaris operating system and Linux platform, this API is dependent on the Gnome libraries. If these libraries are not available, this method will return false.

For platforms that do not support or do not provide java.awt.Desktop , take a look at the BrowserLauncher2 project. It is derived and slightly updated from the BrowserLauncher class , originally written and released by Eric Albert. I successfully used the original BrowserLauncher class in a multi-platform Java application that worked locally with a web browser interface in the early 2000s.

Please note that BrowserLauncher2 is licensed under the GNU Lesser General Public License . If this license is unacceptable, find a copy of the original BrowserLauncher with a very liberal license:

This code is copyright 1999-2001 by Eric Albert (ejalbert@cs.stanford.edu) and may be distributed or modified in any form without restriction until part of this comment from this paragraph has been deleted before the end of the comment. so that it is notified of any application, applet or other binary file that uses this code, but this is more out of curiosity than anything else, and is not required. This software does not include any warranty. The author is not responsible for the loss of data or functionality, as well as for any adverse or unexpected consequences of using this software.

Credits: Stephen Spencer, JavaWorld Magazine (Java Tip 66). Thanks also to Ron B. Ye, Eric Shapiro, Ben Engber, Paul Teitlebaum, Andrea Cantatore, Larry Barowski, Trevor Bedzek, Frank Midrich and Ron Rabakukku

Projects other than BrowserLauncher2 may also have updated the original BrowserLauncher to account for changes in the browser and system default security settings since 2001.

+5
Feb 10 '14 at 11:44
source share

for windows invoke "cmd / k start http://www.example.com " Infact you can always invoke the default programs using the start command. For ex start, abc.mp3 will call the default mp3 player and load the requested mp3 file.

+2
Mar 07 2018-11-11T00:
source share

Its very simple to write below code:

 String s = "http://www.google.com"; Desktop desktop = Desktop.getDesktop(); desktop.browse(URI.create(s)); 

or if you don't want to load the url just write your browser name in string values, e.g.

 String s = "chrome"; Desktop desktop = Desktop.getDesktop(); desktop.browse(URI.create(s)); 

it will automatically open a browser with a blank url after running the program

+2
Mar 15 '17 at 13:43 on
source share



All Articles