Java – How to determine build architecture (32bit / 64bit) with ant

64-bitantbuildjava

We have inherited an ant build file but now need to deploy to both 32bit and 64bit systems.

The non-Java bits are done with GNUMakefiles where we just call "uname" to get the info. Is there a similar or even easier way to mimic this with ant?

Best Answer

Late to the party, but what the heck...

${os.arch} only tells you if the JVM is 32/64bit. You may be running the 32bit JVM on a 64bit OS. Try this:

<var name ="os.bitness" value ="unknown"/>
<if>
<os family="windows"/>
<then>
    <exec dir="." executable="cmd" outputproperty="command.ouput">
        <arg line="/c SET ProgramFiles(x86)"/>
    </exec>
    <if>
        <contains string="${command.ouput}" substring="Program Files (x86)"/>
        <then>
            <var name ="os.bitness" value ="64"/>
        </then>
        <else>
            <var name ="os.bitness" value ="32"/>
        </else>
    </if>
</then>
<elseif>
    <os family="unix"/>
    <then>
        <exec dir="." executable="/bin/sh" outputproperty="command.ouput">
        <arg line="/c uname -m"/>
        </exec>
        <if>
            <contains string="${command.ouput}" substring="_64"/>
            <then>
                <var name ="os.bitness" value ="64"/>
            </then>
            <else>
                <var name ="os.bitness" value ="32"/>
            </else>
        </if>
    </then>
</elseif>
</if>

<echo>OS bitness: ${os.bitness}</echo>

EDIT: As @GreenieMeanie pointed out, this requires the ant-contrib library from ant-contrib.sourceforge.net