gpt4 book ai didi

ant - 创建跨平台Java SWT应用程序

转载 作者:行者123 更新时间:2023-12-03 08:22:59 26 4
gpt4 key购买 nike

我已经使用SWT编写了Java GUI。我使用ANT脚本(下面的片段)打包了应用程序。

<jar destfile="./build/jars/swtgui.jar" filesetmanifest="mergewithoutmain">
<manifest>
<attribute name="Main-Class" value="org.swtgui.MainGui" />
<attribute name="Class-Path" value="." />
</manifest>
<fileset dir="./build/classes" includes="**/*.class" />
<zipfileset excludes="META-INF/*.SF" src="lib/org.eclipse.swt.win32.win32.x86_3.5.2.v3557f.jar" />
</jar>

这将产生一个jar,在Windows上,我只需双击即可运行我的GUI。缺点是我不得不将Windows SWT程序包显式打包到我的jar中。

我希望能够在其他平台(主要是Linux和OS X)上运行我的应用程序。最简单的方法是创建特定于平台的jar,将相应的SWT文件打包到单独的JAR中。

有一个更好的方法吗?是否可以创建一个可以在多个平台上运行的JAR?

最佳答案

我遇到了同样的问题。我还没有尝试过,但是我计划在所有平台上包括swt.jar的版本,并在main方法的开头动态地加载正确的版本。

更新:有效。 build.xml包括所有 jar :

<zipfileset dir="/home/aromanov/workspace/foo/lib" includes="swt_linux_gtk_x86.jar"/>
<zipfileset dir="/home/aromanov/workspace/foo/lib" includes="swt_macosx_x86.jar"/>
<zipfileset dir="/home/aromanov/workspace/foo/lib" includes="swt_win32_x86.jar"/>
<zipfileset dir="/home/aromanov/workspace/foo/lib" includes="swt_linux_gtk_x64.jar"/>
<zipfileset dir="/home/aromanov/workspace/foo/lib" includes="swt_macosx_x64.jar"/>
<zipfileset dir="/home/aromanov/workspace/foo/lib" includes="swt_win32_x64.jar"/>

我的 main方法首先调用此代码:
private void loadSwtJar() {
String osName = System.getProperty("os.name").toLowerCase();
String osArch = System.getProperty("os.arch").toLowerCase();
String swtFileNameOsPart =
osName.contains("win") ? "win32" :
osName.contains("mac") ? "macosx" :
osName.contains("linux") || osName.contains("nix") ? "linux_gtk" :
""; // throw new RuntimeException("Unknown OS name: "+osName)

String swtFileNameArchPart = osArch.contains("64") ? "x64" : "x86";
String swtFileName = "swt_"+swtFileNameOsPart+"_"+swtFileNameArchPart+".jar";

try {
URLClassLoader classLoader = (URLClassLoader) getClass().getClassLoader();
Method addUrlMethod = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
addUrlMethod.setAccessible(true);

URL swtFileUrl = new URL("rsrc:"+swtFileName); // I am using Jar-in-Jar class loader which understands this URL; adjust accordingly if you don't
addUrlMethod.invoke(classLoader, swtFileUrl);
}
catch(Exception e) {
throw new RuntimeException("Unable to add the SWT jar to the class path: "+swtFileName, e);
}
}

[EDIT]对于那些正在寻找“jar-in-jar classloader”的人:它包含在Eclipse的JDT(基于Eclipse构建的Java IDE)中。使用存档器打开 org.eclipse.jdt.ui_*version_number*.jar,您将在其中找到文件 jar-in-jar-loader.zip

关于ant - 创建跨平台Java SWT应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2706222/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com