所有可能的值都在32位JRE和64位Jre中

我需要在Linux,Solaris和Windows上的JRE 1.6中对os.arch属性的所有可能值进行最新编译。 如果可能,请引用您的调查结果来源。 我需要这个值来选择我的JNLP文件中的资源。 基本上我需要根据JRE是32位还是64位来分配不同的JVM内存。 等待你的答复。 谢谢

你也可以写下面的代码来找出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 archMap = new HashMap(); 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

你可以在自己的jdk中找到它的最佳位置。

查看java.lang.System您可以看到使用initProperties方法在initializeSystemClass方法中initializeSystemClass属性,该方法依赖于使用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.c ,看看从java.lang.System.initProperties调用的JNI方法。 来自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); 以及使用java_props_t *sprops = GetJavaProperties(env);实现的java_props_t *sprops = GetJavaProperties(env); 。 所以让我们看一下GetJavaProperties(env) ,这个方法在java_props.h定义为:

java_props_t *GetJavaProperties(JNIEnv *env);

而实施似乎取决于操作系统。

最后看看GetJavaProperties的具体实现; 在Windows中,此属性可以采用的值是ia64amd64x86unknown 。 您可以从java_props_md.c文件中看到:

 #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的java_props_md.c定义的宏,如下所示:

 sprops.os_arch = ARCHPROPNAME; 

这个宏在以下Makefile定义为:

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

所以它看起来像是来自环境,它在哪里编译(抱歉,我不是C专家,我只是猜测,但是我可以指导你一点)。

src/linux/native/的Linux文件夹中没有java_props_md.c所以我想在这种情况下使用与solaris相同的源代码(我再次猜测……)。

注意 :我使用1.6版本来获取此值,但是可以在最新的Java版本中添加新值,因此请检查所需的版本。

希望能帮助到你,