How do I programmatically determine operating system in Java?

后端 未结 19 2567
眼角桃花
眼角桃花 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: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
    \
    

提交回复
热议问题