0

I'm writing an SWT application which can be used on Windows (32/64 bit) and Mac OSX (32/64 bit).

Apart from the JRE I rely on the SWT library found here. I can find four versions of the SWT library depending upon my target platforms (as mentioned above).

When building my application, how can I compile using the correct SWT Jar? If possible, I'd like to try and avoid hard-coding the Jar version, platform and architecture. The SWT Jars are named like this:

  • swt-win32-x86_64.jar
  • swt-win32-x86_32.jar
  • swt-macosx-x86_32.jar
  • swt-macosx-x86_64.jar

(My project will be an open source project. I'd like people to be able to download the source and build it and therefore I've thought of including all the four versions of the SWT Jars in the source distribution. I hope this is the correct approach of publishing code relying on third-part libraries.)

Thanks everyone.

Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
  • 1
    Have you checked these links: http://stackoverflow.com/questions/7134219/swt-jar-for-different-platform http://stackoverflow.com/questions/2706222/create-cross-platform-java-swt-application/. There is custom Ant Task mentioned in the 2nd link. Have you checked that? – Favonius Jun 05 '12 at 10:01
  • Hi Favonius. Those links look helpful. I'll go through them in detail. I've used another fix to solve this issue. My answer is below. What are your thoughts on this method? Thank you. – Mridang Agarwalla Jun 05 '12 at 10:38

1 Answers1

0

I've tried to accomplish it like this: I've installed the Ant Contrib tasks because it supports if statements. I've modified my build file to use to detect the OS platform and architecture.

When compiling, it uses all the 4 SWT Jars from my lib.dir but after compilation, it copies over only the necessary SWT Jar to my build directory. I guess this would keep my size of my final ZIP much smaller than keeping all four JARs.

<target name="copy" depends="compile">
    <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>
                <copy file="${lib.dir}/swt-win32-x86_64.jar" tofile="${jar.dir}/SWT.jar"/>
            </then>
            <else>
                <copy file="${lib.dir}/swt-win32-x86_32.jar" tofile="${jar.dir}/SWT.jar"/>
            </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>
                    <copy file="${lib.dir}/swt-macosx-x86_64.jar" tofile="${jar.dir}/SWT.jar"/>
                </then>
                <else>
                    <copy file="${lib.dir}/swt-macosx-x86_32.jar" tofile="${jar.dir}/SWT.jar"/>
                </else>
            </if>
        </then>
    </elseif>
    </if>
</target>

This seems to work so far. I'll test it some more and add a comment.

Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
  • 1
    You said `When compiling, it uses all the 4 SWT Jars from my lib.dir` the problem I could sense is that you might pickup a version (depending on the order in which you have mentioned them in the classpath) which might not be compatible with the target machine. For example, picking a win32 jar for a macosx, or win64 jar for win32. – Favonius Jun 05 '12 at 11:09
  • You have a point. I'll experiment with the order of the files and it's effects and post here. – Mridang Agarwalla Jun 05 '12 at 11:18