Java judges whether the system is linux or windows system

Determine whether a system is windows or linux?

package com.zhang.systemdemo;
 
import org.junit.jupiter.api.Test;
 
/**
 * @description: 判断运行的系统是windows还是linux
 * @author: zhangyu
 */
public class JudgeSystem {
    public static boolean isLinux() {
        return System.getProperty("os.name").toLowerCase().contains("linux");
    }
 
    public static boolean isWindows() {
        return System.getProperty("os.name").toLowerCase().contains("windows");
    }
 
    public String JudgeSystem() {
        if (isLinux()) {
            return "linux";
        } else if (isWindows()) {
            return "windows";
        } else {
            return "other system";
        }
    }
 
    @Test
    public void fun() {
        boolean flag1 = isLinux();
        // System.out.println(flag1);
        boolean flag2 = isWindows();
        // System.out.println(flag2);
        // System.out.println(System.getProperty("os.name"));
        String sys = JudgeSystem();
        System.out.println(sys);
    }
}

Guess you like

Origin blog.csdn.net/zlfjavahome/article/details/130732533