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 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
krzysiek.ste Mar 02 '15 at 10:07 2015-03-02 10:07
source share