2012-06-01 83 views
27

我需要在Linux,Solaris和Windows上的JRE 1.6中的os.arch屬性的所有可能值的最新編譯。 如果可能的話請引用你的發現的來源。 我需要這個值來選擇我的JNLP文件中的資源。基本上我需要根據JRE是32位還是64位來分配不同的JVM內存。 等待您的答案。 謝謝所有可能的值os.arch在32位JRE和64位Jre

回答

8

你也可以寫下如下代碼來找出os和它的archi。

import java.util.HashMap; 
import java.util.Map; 

import org.apache.commons.lang.SystemUtils; 


public class PlatformDetection { 
    private String os; 
    private String arch; 
    public static String OS_WINDOWS = "windows"; 
    public static String OS_OSX = "osx"; 
    public static String OS_SOLARIS = "solaris"; 
    public static String OS_LINUX = "linux"; 
    public static String ARCH_PPC = "ppc"; 
    public static String ARCH_X86_32 = "x86_32"; 
    public static String ARCH_X86_64 = "x86_64"; 

    public PlatformDetection() { 
     // resolve OS 
     if (SystemUtils.IS_OS_WINDOWS) { 
      this.os = OS_WINDOWS; 
     } else if (SystemUtils.IS_OS_MAC_OSX) { 
      this.os = OS_OSX; 
     } else if (SystemUtils.IS_OS_SOLARIS) { 
      this.os = OS_SOLARIS; 
     } else if (SystemUtils.IS_OS_LINUX) { 
      this.os = OS_LINUX; 
     } else { 
      throw new IllegalArgumentException("Unknown operating system " + SystemUtils.OS_NAME); 
     } 

     // resolve architecture 
     Map<String, String> archMap = new HashMap<String, String>(); 
     archMap.put("x86", ARCH_X86_32); 
     archMap.put("i386", ARCH_X86_32); 
     archMap.put("i486", ARCH_X86_32); 
     archMap.put("i586", ARCH_X86_32); 
     archMap.put("i686", ARCH_X86_32); 
     archMap.put("x86_64", ARCH_X86_64); 
     archMap.put("amd64", ARCH_X86_64); 
     archMap.put("powerpc", ARCH_PPC); 
     this.arch = archMap.get(SystemUtils.OS_ARCH); 
     if (this.arch == null) { 
      throw new IllegalArgumentException("Unknown architecture " + SystemUtils.OS_ARCH); 
     } 
    } 

    public String getOs() { 
     return os; 
    } 

    public String getArch() { 
     return arch; 
    } 

    public void setArch(String arch) { 
     this.arch = arch; 
    } 

    public void setOs(String os) { 
     this.os = os; 
    } 

    public String toString() { 

     return os + "_" + arch; 
    } 
} 

請參閱下面的鏈接

  1. https://github.com/trustin/os-maven-plugin/blob/master/src/main/java/kr/motd/maven/os/Detector.java

  2. https://github.com/rachelxqy/EligibilityCriteriaModeling/blob/57001f6d86084f074f4ca6aaff157e93ef6abf95/src/main/java/edu/mayo/bmi/medtagger/ml/util/PlatformDetection.java

+0

我沒有給這個答案的賞金,因爲它不清楚鏈接是多麼可靠。他們爲一些相同的體系結構(例如'powerpc' vs'ppc')檢測不同的'os.arch'值,他們是否知道這些值是否會出現或者是否是迷信編碼尚不清楚。你也在這裏複製粘貼其中一個,我不知道它的版權狀態是什麼。 – Boann

6

在那裏你可以看看這個它在自己的JDK的最佳地點。

看好java.lang.System可以看到的性質在initializeSystemClass方法使用initProperties方法,它依賴於本地代碼使用JNI初始化:

private static native Properties initProperties(Properties props); 

/** 
* Initialize the system class. Called after thread initialization. 
*/ 
private static void initializeSystemClass() { 

    // VM might invoke JNU_NewStringPlatform() to set those encoding 
    // sensitive properties (user.home, user.name, boot.class.path, etc.) 
    // during "props" initialization, in which it may need access, via 
    // System.getProperty(), to the related system encoding property that 
    // have been initialized (put into "props") at early stage of the 
    // initialization. So make sure the "props" is available at the 
    // very beginning of the initialization and all system properties to 
    // be put into it directly. 
    props = new Properties(); 
    initProperties(props); // initialized by the VM 
    ... 
    ... 
} 

如果選中的這個原生代碼initProperties呼籲源您可以在不同的平臺上查看os.arch系統屬性的可能值。所以一步一步地做:

首先看System.cJNI方法調用java.lang.System.initProperties。從System.c

JNIEXPORT jobject JNICALL 
Java_java_lang_System_initProperties(JNIEnv *env, jclass cla, jobject props) 
{ 
    char buf[128]; 
    java_props_t *sprops = GetJavaProperties(env); 
    jmethodID putID = (*env)->GetMethodID(env, 
              (*env)->GetObjectClass(env, props), 
              "put", 
      "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); 

    if (sprops == NULL || putID == NULL) return NULL; 

    PUTPROP(props, "java.specification.version", 
      JDK_MAJOR_VERSION "." JDK_MINOR_VERSION); 
    PUTPROP(props, "java.specification.name", 
      "Java Platform API Specification"); 
    PUTPROP(props, "java.specification.vendor", "Sun Microsystems Inc."); 

    PUTPROP(props, "java.version", RELEASE); 
    PUTPROP(props, "java.vendor", VENDOR); 
    PUTPROP(props, "java.vendor.url", VENDOR_URL); 
    PUTPROP(props, "java.vendor.url.bug", VENDOR_URL_BUG); 

    ... 

    /* os properties */ 
    PUTPROP(props, "os.name", sprops->os_name); 
    PUTPROP(props, "os.version", sprops->os_version); 

    // HERE IS THE `os.arch` PROPERTY :) 

    PUTPROP(props, "os.arch", sprops->os_arch); 

因此,大家可以看到os.arch來自PUTPROP(props, "os.arch", sprops->os_arch);和它的實現sprops使用java_props_t *sprops = GetJavaProperties(env);。所以讓我們來看看GetJavaProperties(env),這種方法是在java_props.h定義爲:

java_props_t *GetJavaProperties(JNIEnv *env);

和實現似乎取決於操作系統。

所以最後看一下GetJavaProperties的具體實現; 在Windows中,此屬性可能採取的值爲ia64amd64,x86unknown。您可以從java_props_md.c file看到:

#if _M_IA64 
     sprops.os_arch = "ia64"; 
#elif _M_AMD64 
     sprops.os_arch = "amd64"; 
#elif _X86_ 
     sprops.os_arch = "x86"; 
#else 
     sprops.os_arch = "unknown"; 
#endif 

對於Solaris似乎更復雜,因爲在本地代碼的屬性值來自於​​具體定義用於Solaris作爲宏:

sprops.os_arch = ARCHPROPNAME; 

而這個宏是在後續Makefile定義爲:

OTHER_CPPFLAGS += -DARCHPROPNAME='"$(ARCHPROP)"'

所以看起來就像這個來自環境,編譯它的地方(對不起,我不是C專家,我只是在猜測,但也許我可以指導你)。

src/linux/native/的Linux文件夾中沒有java_props_md.c,所以我想在這種情況下采取與solaris相同的來源(我再次猜測......)。

注意:我使用1.6版本來獲得這個值,但是新的值可以被添加到最新的java版本中,所以檢查你需要的版本。

希望它有幫助,

+0

雖然這不是一個完整的解決方案,但我給了它的賞金,因爲你努力尋找規範值。注意:由於歷史原因,在jdk9之前的Java文件夾中,Solaris文件夾包含大部分通用的Unix資料,所以是的,該代碼將適用於Linux。如果價值來自環境,這很不幸,因爲這意味着它的價值是*開放式*。我希望有一個可能的價值整潔,規範的表格,而不需要迷信許多建築物的同義詞,但也許這是不可能的。 – Boann

+0

@Boann感謝您對solaris和linux的確認。起初,我在查看文檔來查看'os.arch'的可能值,但似乎沒有關於它的官方信息......所以我直接看代碼;不幸的是,這也不能提供我們正在尋找的信息。無論如何,感謝賞金':)'。 – albciff