All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.aequologica.neo.shakuntala.utils.OpenBrowserInJavaWindowsOrLinux Maven / Gradle / Ivy

package net.aequologica.neo.shakuntala.utils;

import java.io.IOException;

/**
 * http://www.mkyong.com/java/open-browser-in-java-windows-or-linux/
 */
public class OpenBrowserInJavaWindowsOrLinux {

    public static void main(String args[]) throws IOException {

        String url;
        if (args.length < 1) {
            url = "http://www.google.com";
        } else {
            url = args[0];
        }

        openBrowserInJavaWindowsOrLinux(url);
    }

    public static void openBrowserInJavaWindowsOrLinux(String url) throws IOException {
        String  os = System.getProperty("os.name").toLowerCase();
        Runtime rt = Runtime.getRuntime();

        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;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy