This article shows a handy Java class that uses System.getProperty("os.name") to detect which type of operating system (OS) you are using now. Then we provide a very useful Java code, to open a web browser from Java application in windows or Linux.

1. Detect OS (Original Version)

This code can detect WindowsMacUnix, and Solaris.

OSValidator.java

package com.favtuts.system;

public class OSValidator {
    
    private static String OS = System.getProperty("os.name").toLowerCase();    
    public static void main(String[] args) {
        
        System.out.println("os.name: " + OS);

        if (isWindows()) {
            System.out.println("This is Windows");
        } else if (isMac()) {
            System.out.println("This is Mac");
        } else if (isUnix()) {
            System.out.println("This is Unix or Linux");
        } else if (isSolaris()) {
            System.out.println("This is Solaris");
        } else {
            System.out.println("Your OS is not support!!");
        }
    }

    public static boolean isWindows() {
        return (OS.indexOf("win") >= 0);
    }

    public static boolean isMac() {
        return (OS.indexOf("mac") >= 0);
    }

    public static boolean isUnix() {
        return (OS.indexOf("nix") >= 0
                || OS.indexOf("nux") >= 0
                || OS.indexOf("aix") > 0);
    }

    public static boolean isSolaris() {
        return (OS.indexOf("sunos") >= 0);
    }
}

Output, run on Windows 10.

os.name: windows 10
This is Windows

2. Detect OS (Enhanced Version)

Since the operating system will remain the same for the running Java app, we can increase the performance by moving the OS checking to static fields; The static ensures the OS.indexOf checking runs once only.

OSValidator.java

package com.favtuts.system;

public class OSValidator {

    private static String OS = System.getProperty("os.name").toLowerCase();
    public static boolean IS_WINDOWS = (OS.indexOf("win") >= 0);
    public static boolean IS_MAC = (OS.indexOf("mac") >= 0);
    public static boolean IS_UNIX = (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0);
    public static boolean IS_SOLARIS = (OS.indexOf("sunos") >= 0);

    public static void main(String[] args) {

        System.out.println("os.name: " + OS);

        if (IS_WINDOWS) {
            System.out.println("This is Windows");
        } else if (IS_MAC) {
            System.out.println("This is Mac");
        } else if (IS_UNIX) {
            System.out.println("This is Unix or Linux");
        } else if (IS_SOLARIS) {
            System.out.println("This is Solaris");
        } else {
            System.out.println("Your OS is not support!!");
        }
    }

}

3. Open Browser

A very useful Java code, to open a web browser from Java application in windows or Linux.

StartBrowser.java

package com.favtuts.system;

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

}

Download Source Code

$ git clone https://github.com/favtuts/java-core-tutorials-examples

$ cd java-misc/system

References

Leave a Reply

Your email address will not be published. Required fields are marked *