java 32 位 JRE 和 64 位 Jre 中的所有可能值 os.arch
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10846105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
All possible values os.arch in 32bit JRE and in 64bit Jre
提问by Brahma
I need latest compilation of all possible values of the os.arch property in JRE 1.6 on Linux,Solaris and Windows. If possible please Quote the source of your findings. I need this values to select resources in my JNLP file. Basically I need to assign different JVM memory based on whether the JRE is 32bit or 64bit. Waiting for your answer. Thanks
我需要在 Linux、Solaris 和 Windows 上的 JRE 1.6 中对 os.arch 属性的所有可能值进行最新编译。如果可能,请引用您的发现的来源。我需要这些值来选择 JNLP 文件中的资源。基本上我需要根据 JRE 是 32 位还是 64 位来分配不同的 JVM 内存。等待你的答复。谢谢
回答by albciff
The best place where you can look for this it's in the own jdk.
您可以在自己的 jdk 中找到它的最佳位置。
Looking on java.lang.System
you can see that the properties are initialized in initializeSystemClass
method using initProperties
method which relies on native code using JNI
:
查看java.lang.System
您可以看到属性是在initializeSystemClass
方法 using中初始化的,该 方法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
...
...
}
If you check the source of this native code called from initProperties
for the different platforms you can see the possible values for os.arch
system property. So do it step by step:
如果您检查从initProperties
不同平台调用的本机代码的来源,您可以看到os.arch
系统属性的可能值。所以一步一步来:
First look at System.c
to see the JNI
method called from java.lang.System.initProperties
. From System.c
先看一下System.c
,看JNI
调用 from的方法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);
So as you can see the os.arch
comes from PUTPROP(props, "os.arch", sprops->os_arch);
and the sprops
it's achieved using java_props_t *sprops = GetJavaProperties(env);
. so lets look at GetJavaProperties(env)
, this method it's defined in java_props.h
as:
所以你可以看到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);
java_props_t *GetJavaProperties(JNIEnv *env);
And the implementation seems that depends on OS.
并且实现似乎取决于操作系统。
So finally looking a specific implementation for GetJavaProperties
;
in Windows the possible values which this property can take are ia64
, amd64
, x86
, or unknown
. You can see from java_props_md.c
file:
所以最后寻找一个具体的实现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
For Solaris seems more complicated since the property value in the native code comes from a Macro defined in the java_props_md.c
specific for solaris as:
对于 Solaris 似乎更复杂,因为本机代码中的属性值来自在java_props_md.c
特定于 solaris 中定义的宏:
sprops.os_arch = ARCHPROPNAME;
And this Macro it's defined in the follow Makefile
as:
这个宏定义如下Makefile
:
OTHER_CPPFLAGS += -DARCHPROPNAME='"$(ARCHPROP)"'
OTHER_CPPFLAGS += -DARCHPROPNAME='"$(ARCHPROP)"'
So it looks like this comes from the environment, where it's compiled (sorry I'm not a C expert, I'm just guessing however maybe I can guide you a bit).
所以看起来这来自环境,它是编译的(对不起,我不是 C 专家,我只是猜测,但也许我可以指导你一点)。
In the Linux folder in src/linux/native/
there is no java_props_md.c
so I suppose that in this case take the same source as solaris (I'm guessing again...).
在 Linux 文件夹中src/linux/native/
没有,java_props_md.c
所以我想在这种情况下采用与 solaris 相同的源(我再次猜测......)。
NOTE: I use the 1.6 version to get this values, however new values can will be added in newest java versions, so check your required version.
注意:我使用 1.6 版本来获取此值,但是可以在最新的 Java 版本中添加新值,因此请检查您所需的版本。
Hope it helps,
希望能帮助到你,
回答by Pratiyush Kumar Singh
You can also write some code like below to find out os and its archi.
你也可以写一些像下面这样的代码来找出 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;
}
}
Refer below Links
参考以下链接
回答by Malt
I ran into the same problem in 2019. Especially with regard to arm processors.
我在 2019 年遇到了同样的问题。尤其是在 arm 处理器方面。
After trying it out, the raspberry pi 2 (ARMv7) seems to simply return the string arm
.
尝试之后,树莓派 2 (ARMv7) 似乎只是简单地返回了 string arm
。
The raspberry pi 3 (ARMv8) returns aarch64
.
树莓派 3 (ARMv8) 返回aarch64
.
x86 64-bit desktops and servers return amd64
.
x86 64 位台式机和服务器返回amd64
.
Hope this helps someone.
希望这可以帮助某人。