[转] 关于System.getProperty("os.arch")

[b][color=red][size=large]Java's "os.arch" System Property is the Bitness of the JRE, NOT the Operating System[/size][/color][/b]
[color=blue]By Mark Kolich on October 19, 2009 11:15 AM | No TrackBacks[/color]

原文:[url]http://mark.koli.ch/2009/10/javas-osarch-system-property-is-the-bitness-of-the-jre-not-the-operating-system.html[/url]
参考:[url]http://lopica.sourceforge.net/os.html[/url]

If you ever use Java to check if a system is 32 or 64-bit, you should know that Java's "os.arch" system property returns the bitness of the JRE, not the OS itself. Sites like this are WRONG; any resource that claims Java's "os.arch" property returns the real "architecture of the OS" is lying. Case in point, I recently ran this tiny program on a 64-bit Windows 7 machine, with a 32-bit JRE:

import com.sun.servicetag.SystemEnvironment;

public class OSArchLies {

public static void main(String[] args) {

// Will say "x86" even on a 64-bit machine
// using a 32-bit Java runtime
SystemEnvironment env =
SystemEnvironment.getSystemEnvironment();
final String envArch = env.getOsArchitecture();

// The os.arch property will also say "x86" on a
// 64-bit machine using a 32-bit runtime
final String propArch = System.getProperty("os.arch");

System.out.println( "getOsArchitecture() says => " + envArch );
System.out.println( "getProperty() says => " + propArch );

}

}


The output from this tiny app on a 64-bit box:

#/> java OSArchLies
getOsArchitecture() says => x86
getProperty() says => x86

In this case, one would expect to see something like "x86_64" or "amd64" instead of just "x86". Bottom line, don't believe what you read online about "os.arch" and other Java system properties. They are usually properties of the JRE/JDK itself, and not necessarily the real properties of the underlying OS or architecture. If you need to check if a system is actually 32 or 64-bit, you should look elsewhere in the system registry or write your own native app and call it from Java.[url]http://mark.koli.ch/2009/10/reliably-checking-os-bitness-32-or-64-bit-on-windows-with-a-tiny-c-app.html[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值