Java 判断 OS 类型

    1. public class OSValidator{
    2. public static void main(String[] args)
    3. {
    4. if(isWindows()){
    5. System.out.println("This is Windows");
    6. }else if(isMac()){
    7. System.out.println("This is Mac");
    8. }else if(isUnix()){
    9. System.out.println("This is Unix or Linux");
    10. }else{
    11. System.out.println("Your OS is not support!!");
    12. }
    13. }
    14. public static boolean isWindows(){
    15. String os = System.getProperty("os.name").toLowerCase();
    16. //windows
    17. return (os.indexOf( "win" ) >= 0);
    18. }
    19. public static boolean isMac(){
    20. String os = System.getProperty("os.name").toLowerCase();
    21. //Mac
    22. return (os.indexOf( "mac" ) >= 0);
    23. }
    24. public static boolean isUnix(){
    25. String os = System.getProperty("os.name").toLowerCase();
    26. //linux or unix
    27. return (os.indexOf( "nix") >=0 || os.indexOf( "nux") >=0);
    28. }
    29. }