简体   繁体   English

减少SWT跨平台jar的大小

[英]reducing SWT cross-platform jar size

I have made a cross-platform SWT jar using the clear explanation I found on: Create cross platform Java SWT Application 我已经使用以下清楚的说明制作了一个跨平台的SWT jar: 创建跨平台Java SWT应用程序

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. 尽管如此,这仍然需要我打包每个平台的jar,以便使其独立于系统,从而使jar的总大小约为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. 我曾尝试使用ProGuard减小文件大小,但这不是很有用。 Can I conclude from this that it is in principle not possible to create small cross-platform applications using SWT? 我可以由此得出结论,原则上不可能使用SWT创建小型跨平台应用程序吗?

Well, you could make your program not include the SWT jar and download the correct one during installation or first run. 好吧,您可以使您的程序不包含SWT jar,并在安装或首次运行时下载正确的jar。 Probably not a good idea, but possible. 可能不是一个好主意,但可能。 Otherwise, I'd just use Swing in this situation. 否则,我只会在这种情况下使用Swing。

It is possible to extract class files that are common between all desired platforms into a separate jar. 可以将所有所需平台之间通用的类文件提取到单独的jar中。 Then remove these common classes from the platform specific jars. 然后从平台特定的jar中删除这些通用类。

At runtime you add both the common jar and the platform specific jar to the URLClassLoader. 在运行时,将通用jar和特定于平台的jar添加到URLClassLoader。

Works for me. 为我工作。

Here is an old shell script I used to repack the jars, I hope it works: 这是我用来重新包装罐子的旧的shell脚本,希望它能起作用:

#!/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 4.3版的最终jar大小为:

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). 还要记住,为MacOS创建一个单击运行的样式SWT应用程序很棘手(-XstartOnFirstThread选项问题)。 So you might consider removing the MacOS platform. 因此,您可以考虑删除MacOS平台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM