How do I programmatically determine operating system in Java?

后端 未结 19 2518
眼角桃花
眼角桃花 2020-11-22 04:56

I would like to determine the operating system of the host that my Java program is running programmatically (for example: I would like to be able to load different propertie

相关标签:
19条回答
  • 2020-11-22 05:08

    If you're working in a security sensitive environment, then please read this through.

    Please refrain from ever trusting a property obtained via the System#getProperty(String) subroutine! Actually, almost every property including os.arch, os.name, and os.version isn't readonly as you'd might expect — instead, they're actually quite the opposite.

    First of all, any code with sufficient permission of invoking the System#setProperty(String, String) subroutine can modify the returned literal at will. However, that's not necessarily the primary issue here, as it can be resolved through the use of a so called SecurityManager, as described in greater detail over here.

    The actual issue is that any user is able to edit these properties when running the JAR in question (through -Dos.name=, -Dos.arch=, etc.). This means that there's no way to determine if these properties are indeed actually accurate. Because of this, here are a few additional checks to try and avoid tampering:

    // The first thing we're able to do is to query the filesystem.
    switch (java.io.File.separator)
    {
        case "/":
            // Windows is a potential candidate.
            break;
        case "\\":
            // And here it could really be anything else.
            break;
        default:
            // There's probably something really wrong here by now.
            break;
    }
    
    0 讨论(0)
  • 2020-11-22 05:09

    Below code shows the values that you can get from System API, these all things you can get through this API.

    public class App {
        public static void main( String[] args ) {
            //Operating system name
            System.out.println(System.getProperty("os.name"));
    
            //Operating system version
            System.out.println(System.getProperty("os.version"));
    
            //Path separator character used in java.class.path
            System.out.println(System.getProperty("path.separator"));
    
            //User working directory
            System.out.println(System.getProperty("user.dir"));
    
            //User home directory
            System.out.println(System.getProperty("user.home"));
    
            //User account name
            System.out.println(System.getProperty("user.name"));
    
            //Operating system architecture
            System.out.println(System.getProperty("os.arch"));
    
            //Sequence used by operating system to separate lines in text files
            System.out.println(System.getProperty("line.separator"));
    
            System.out.println(System.getProperty("java.version")); //JRE version number
    
            System.out.println(System.getProperty("java.vendor.url")); //JRE vendor URL
    
            System.out.println(System.getProperty("java.vendor")); //JRE vendor name
    
            System.out.println(System.getProperty("java.home")); //Installation directory for Java Runtime Environment (JRE)
    
            System.out.println(System.getProperty("java.class.path"));
    
            System.out.println(System.getProperty("file.separator"));
        }
    }
    

    Answers:-

    Windows 7
    6.1
    ;
    C:\Users\user\Documents\workspace-eclipse\JavaExample
    C:\Users\user
    user
    amd64
    
    
    1.7.0_71
    http://java.oracle.com/
    Oracle Corporation
    C:\Program Files\Java\jre7
    C:\Users\user\Documents\workspace-Eclipse\JavaExample\target\classes
    \
    
    0 讨论(0)
  • 2020-11-22 05:10

    The following JavaFX classes have static methods to determine current OS (isWindows(),isLinux()...):

    • com.sun.javafx.PlatformUtil
    • com.sun.media.jfxmediaimpl.HostUtils
    • com.sun.javafx.util.Utils

    Example:

    if (PlatformUtil.isWindows()){
               ...
    }
    
    0 讨论(0)
  • 2020-11-22 05:11

    You can use:

    System.getProperty("os.name")
    

    P.S. You may find this code useful:

    class ShowProperties {
        public static void main(String[] args) {
            System.getProperties().list(System.out);
        }
    }
    

    All it does is print out all the properties provided by your Java implementations. It'll give you an idea of what you can find out about your Java environment via properties. :-)

    0 讨论(0)
  • 2020-11-22 05:12

    A small example of what you're trying to achieve would probably be a class similar to what's underneath:

    import java.util.Locale;
    
    public class OperatingSystem
    {
        private static String OS = System.getProperty("os.name", "unknown").toLowerCase(Locale.ROOT);
    
        public static boolean isWindows()
        {
            return OS.contains("win");
        }
    
        public static boolean isMac()
        {
            return OS.contains("mac");
        }
    
        public static boolean isUnix()
        {
            return OS.contains("nux");
        }
    }
    

    This particular implementation is quite reliable and should be universally applicable. Just copy and paste it into your class of choice.

    0 讨论(0)
  • 2020-11-22 05:13

    This code for displaying all information about the system os type,name , java information and so on.

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Properties pro = System.getProperties();
        for(Object obj : pro.keySet()){
            System.out.println(" System  "+(String)obj+"     :  "+System.getProperty((String)obj));
        }
    }
    
    0 讨论(0)
提交回复
热议问题