0

I have made a cross-platform SWT jar using the clear explanation I found on: Create cross platform Java SWT Application

Still, this requires me to pack the jars of every platform in order to make it system independent, making the total size of the jar around 40MB. This is somewhat crazy for a project that does some parsing.

I have tried using ProGuard to reduce the file size, but this was not very useful. Can I conclude from this that it is in principle not possible to create small cross-platform applications using SWT?

Community
  • 1
  • 1
marczoid
  • 1,365
  • 2
  • 12
  • 20

2 Answers2

0

Well, you could make your program not include the SWT jar and download the correct one during installation or first run. Probably not a good idea, but possible. Otherwise, I'd just use Swing in this situation.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • 1
    Thanks, but using Swing is rather hard because my entire project has been made using SWT... I have now been able to narrow the packages down to around 10 mb because SWT includes many packages that I didn't need. Still, I think this is too big :-) – marczoid Aug 01 '12 at 08:17
0

It is possible to extract class files that are common between all desired platforms into a separate jar. Then remove these common classes from the platform specific jars.

At runtime you add both the common jar and the platform specific jar to the URLClassLoader.

Works for me.

Here is an old shell script I used to repack the jars, I hope it works:

#!/bin/bash

ref=""
for jar in *.jar ; do
    base="${jar%%.jar}"
    mkdir "$base"
    ( cd "$base" ; unzip -o ../${jar} ;)
    ref="$base"
done

mkdir common
echo "Base is ${ref}"

( cd "$ref" ; find . -type f ) | while read f ; do
    fd5=`cat "${ref}/${f}" | md5sum`
    echo "Processing ${f}..."
    same="yes"
    for jar in *.jar ; do
        if [[ "x${same}" == "xyes" ]] ; then
            base="${jar%%.jar}"
            if [[ -f "${base}/${f}" ]] ; then
                fd5b=`cat "${base}/${f}" | md5sum `
                if [[ "x${fd5}" == "x${fd5b}" ]] ; then
                    echo " - same ${fd5} == ${fd5b} in ${base}"
                else
                    echo " - different ${fd5} != ${fd5b} in ${base}"
                    same="no"
                fi
            else
                echo " - missing in ${base}"
                same="no"
            fi
        fi
    done
    if [[ "x${same}" == "xyes" ]] ; then
        echo " - IDENTICAL"
        d=`dirname "${f}"`
        mkdir -p "common/${d}"
        cp "${ref}/${f}" "common/${f}"
        for jar in *.jar ; do
            base="${jar%%.jar}"
            rm "${base}/${f}"
        done
    else
        echo " - DIFFERENT"
    fi
done

mkdir jars
( cd "common" ; jar -cvf "../jars/common.jar" * ; )
for jar in *.jar ; do
    base="${jar%%.jar}"
    ( cd "$base" ; jar -cvfm "../jars/${jar}" META-INF/MANIFEST.MF * ; )
done

The resulting jar sizes for SWT version 4.3 are:

swt_common.jar 521865
swt_linux_x64.jar 1373413
swt_linux_x86.jar 1222447
swt_macosx_x64.jar 1416943
swt_macosx_x86.jar 1514651
swt_win32_x64.jar 1434927
swt_win32_x86.jar 1421738

Also remember, that doing a click-jar-to-run style SWT application for MacOS is tricky (the -XstartOnFirstThread option issue). So you might consider removing the MacOS platform.

vlp
  • 7,811
  • 2
  • 23
  • 51