[英]All possible values os.arch in 32bit JRE and in 64bit Jre
我需要在 Linux、Solaris 和 Windows 上的 JRE 1.6 中 os.arch 屬性的所有可能值的最新編譯。 如果可能,請引用您的發現的來源。 我需要這些值到我的 JNLP 文件中的 select 資源。 基本上我需要根據JRE是32位還是64位來分配不同的JVM memory。 等待你的答復。 謝謝
你可以在自己的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);
和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
對於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版本中可以添加新值,因此請檢查所需的版本。
希望能幫助到你,
我在 2019 年遇到了同樣的問題。尤其是關於 arm 處理器。
嘗試后,樹莓派 2 (ARMv7) 似乎只是返回字符串arm
。
樹莓派 3 (ARMv8) 返回aarch64
。
x86 64 位台式機和服務器返回amd64
。
希望這可以幫助某人。
你也可以寫下面的代碼來找出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;
}
}
請參閱以下鏈接
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.