2013-05-14 38 views
2

我希望能夠動態確定我的Java應用程序中的操作系統,體系結構和位置。在java中確定os體系結構和位數

例如,我希望我的應用程序知道它何時位於Solaris 32位sparc計算機上或何時位於x86計算機上。

我知道系統get屬性返回JVM的拱形,但是有沒有真正找到真正的os arch和bit-ness?

+0

你爲什麼想要這樣做? Java的主要raisons d'être屏蔽了你的這些東西... – ffxtian 2013-05-14 17:36:23

+0

我添加了一些使用外部庫的功能,但這些庫取決於操作系統。 – Seephor 2013-05-14 17:38:20

回答

2

獲取字節順序的最簡單方法是使用ByteOrder.nativeOrder();

操作系統在系統屬性中。

要確定JVM是否是64位,我使用從ehcache中獲得的以下內容。

private static final boolean IS64BIT = is64Bit0(); 

public static boolean is64Bit() { 
    return IS64BIT; 
} 

private static boolean is64Bit0() { 
    String systemProp; 
    systemProp = System.getProperty("com.ibm.vm.bitmode"); 
    if (systemProp != null) { 
     return systemProp.equals("64"); 
    } 
    systemProp = System.getProperty("sun.arch.data.model"); 
    if (systemProp != null) { 
     return systemProp.equals("64"); 
    } 
    systemProp = System.getProperty("java.vm.version"); 
    return systemProp != null && systemProp.contains("_64"); 
} 
+0

非常感謝你。沒有考慮檢查字節順序。很有幫助。 – Seephor 2013-05-14 18:23:37

+0

@Seephor通常最好在你不需要知道這類事情的地方編寫代碼。 – 2013-05-14 18:29:23

+1

我同意,但我必須利用一些依賴於平臺的外部庫。 – Seephor 2013-05-14 18:48:57