実行中の JVM のビットモード判定

実行中の JVM が 32bit なのか 64bit なのかを判定したい。

以下のエントリのメソッドがシンプルに実装されていて良い感じ。

上記エントリにも書かれているが、プロパティの名前と意味がわかりづらい。

System Properties (The Java™ Tutorials > Essential Classes > The Platform Environment) には、os.arch は Operating system architecture と説明されているが、実際には Java VM のビットモードだ。ドキュメントが間違っているのなら、訂正して欲しいものだ。



さて、上記エントリの実装は一部間違っているように思えるので、修正版。

    /**
     * 32BitOS,64BitOS判別.
     * @return int 32ビットの場合は、32,64ビットの場合は、64が返されます.
     *             -1が返された場合は、不明です.
     */
//    publicstatic final int getOsBit() { // スペースがない
    public static final int getOsBit() {
//        String os = System.getProperty( "sun.arch.data.mode" ) ; mode ではなく model
        String os = System.getProperty( "sun.arch.data.model" ) ;
        if( os != null && ( os = os.trim() ).length() > 0 ) {
            if( "32".equals( os ) ) {
                return 32 ;
            }
            else if( "64".equals( os ) ) {
                return 64 ;
            }
        }
        os = System.getProperty( "os.arch" ) ;
        if( os == null || ( os = os.trim() ).length() <= 0 ) {
            return -1 ;
        }
//        if( os.endsWith( "32" ) ) { // 32 ではなく 86
        if( os.endsWith( "86" ) ) {
            return 32 ;
        }
        else if( os.endsWith( "64" ) ) {
            return 64 ;
        }
        return 32 ;
    }


その他の判別方法は以下が詳しい。