在32位JRE和64位Jre中的所有可能值os.arch.

Bra*_*hma 31 java jnlp

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

alb*_*iff 13

你可以在自己的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
    ...
    ...
}
Run Code Online (Sandbox Code Playgroud)

如果检查从initProperties不同平台调用的本机代码的来源,则可以看到os.arch系统属性的可能值.所以一步一步来做:

首先看看System.c从中JNI调用的方法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);
Run Code Online (Sandbox Code Playgroud)

所以你可以看到它的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此属性可以接受的可能值是ia64,amd64,x86,或unknown.你可以从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
Run Code Online (Sandbox Code Playgroud)

对于Solaris似乎更复杂,因为本机代码中的属性值来自java_props_md.c特定于solaris中定义的宏:

sprops.os_arch = ARCHPROPNAME;
Run Code Online (Sandbox Code Playgroud)

而这个宏在下面定义Makefile为:

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

所以它看起来像是来自环境,它被编译(对不起,我不是C专家,我只是猜测,但是我可以指导你一点).

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

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

希望能帮助到你,


Mal*_*alt 10

我在 2019 年遇到了同样的问题。特别是关于 ARM 处理器。

尝试后,树莓派 2 (ARMv7) 似乎只是返回 string arm

树莓派 3 (ARMv8) 回归aarch64

x86 64 位桌面和服务器返回amd64.

希望这对某人有帮助。


Pra*_*ngh 7

你也可以写下面的代码来找出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;
    }
}
Run Code Online (Sandbox Code Playgroud)

请参阅以下链接

  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