2013-01-11 16 views
98

Usiłuję uruchomić wspólny program Java zarówno w systemie Windows, jak i Linux.Wykrywanie systemu Windows lub Linux?

Program musi działać w różny sposób na każdej platformie.

Jak można/powinienem wykryć, że mój program Java działa pod Linuksem a systemem Windows?

+3

* "Program musi działać w różny sposób na każdej platformie." * Co konkretnie? –

Odpowiedz

9

Można użyć "system.properties.os", na przykład:

public class GetOs { 

    public static void main (String[] args) { 
    String s = 
     "name: " + System.getProperty ("os.name"); 
    s += ", version: " + System.getProperty ("os.version"); 
    s += ", arch: " + System.getProperty ("os.arch"); 
    System.out.println ("OS=" + s); 
    } 
} 

// EXAMPLE OUTPUT: OS=name: Windows 7, version: 6.1, arch: amd64 

Oto więcej szczegółów:

35

Przydatne proste klasy są rozwidlone przeze mnie na: https://gist.github.com/kiuz/816e24aa787c2d102dd0

public class OSValidator { 

    private static String OS = System.getProperty("os.name").toLowerCase(); 

    public static void main(String[] args) { 

     System.out.println(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); 
    } 
    public static String getOS(){ 
     if (isWindows()) { 
      return "win"; 
     } else if (isMac()) { 
      return "osx"; 
     } else if (isUnix()) { 
      return "uni"; 
     } else if (isSolaris()) { 
      return "sol"; 
     } else { 
      return "err"; 
     } 
    } 

} 
+2

Dzięki. Oszczędność czasu. –

+13

'OS.indexOf (" ... ")> = 0' może być zamienione na' OS.contains ("...") ' – kazy

+0

bardzo dziękuję Kazy –

19

myślę, że jest to najlepsze podejście do wykorzystania zależność Apache lang aby zdecydować, który system operacyjny jest uruchomiony programowo za pośrednictwem Java

import org.apache.commons.lang3.SystemUtils; 

public class App { 
    public static void main(String[] args) { 
     if(SystemUtils.IS_OS_WINDOWS_7) 
      System.out.println("It's a Windows 7 OS"); 
     if(SystemUtils.IS_OS_WINDOWS_8) 
      System.out.println("It's a Windows 8 OS"); 
     if(SystemUtils.IS_OS_LINUX) 
      System.out.println("It's a Linux OS"); 
     if(SystemUtils.IS_OS_MAC) 
      System.out.println("It's a MAC OS"); 
    } 
} 
+2

if (File.separatorChar == '\\') windows = true; –

+3

Lub po prostu "SystemUtils.IS_OS_WINDOWS". – SantiBailors

Powiązane problemy