如何使用 SWT 创建在所有平台上运行的可执行 JAR?

发布于 2024-08-17 01:20:13 字数 190 浏览 3 评论 0原文

SWT 附带一个基本 JAR 和每个平台(Windows、Linux/32 位、Linux/64 位、Mac、AIX...)一个特定的 JAR。如何创建一个可执行 JAR 以在运行时选择正确的平台 JAR?

[编辑] 我正在考虑在子目录中提供所有平台 JAR,然后在 main() 中修改类加载器。有人已经尝试过这个了吗?

SWT comes with a base JAR and one specific JAR per platform (Windows, Linux/32bit, Linux/64bit, Mac, AIX, ...). How can I create an executable JAR that will select the correct platform JAR at runtime?

[EDIT] I was thinking to supply all platform JARs in a subdirectory and in main() would then modify the class loader. Has anyone already tried this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

星星的轨迹 2024-08-24 01:20:13

对于我当前的工作,我需要提供一个可执行的 jar,它可以在其内部加载 jar 并执行第二个 main()。基本上是引导程序 main() 和应用程序 main()。

步骤 1. 在清单“main-class”中,将引导类放入

步骤 2. 当引导类运行时,它会将其自己的 jar 及其内部的所有 jar 解压缩到临时目录中。使用类似下面的行来获取您自己的罐子。

Main.class.getProtectionDomain().getCodeSource().getLocation().toURI()

步骤 3. 您的引导类通过“os.name”属性检测操作系统,并使用

private static void loadJarIntoClassloader( URL u ) throws Exception
{
    URLClassLoader sysLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();

    Class<URLClassLoader> sysclass = URLClassLoader.class;
    Method method = sysclass.getDeclaredMethod("addURL", URL.class);
    method.setAccessible(true);
    method.invoke(sysLoader, new Object[]{u});
}

步骤 4 从临时目录加载适当的 jar。现在您应该能够通过调用应用程序 main() 来运行您的应用程序。

注意:这个小技巧取决于您的 JVM 使用 URLClassLoader 作为其 SystemClassLoader,这对于 Sun JVM 来说是正确的,但对于其他 JVM 则不确定。

这样您就可以只提供一个 jar,它会自行解压并使用正确的 jar 运行。

For my current job I needed to supply an executable jar that could load jars inside itself and execute a second main(). Basically a bootstrap main() and an application main().

Step 1. in the manifest "main-class" you put your bootstrap class

Step 2. When your bootstrap class runs it unjar's its own jar and all jars inside it to a temp directory. Use something like the line below to get your own jar.

Main.class.getProtectionDomain().getCodeSource().getLocation().toURI()

Step 3. Your bootstrap class detects the OS via the "os.name" property and loads the appropriate jars from the temp directory with this

private static void loadJarIntoClassloader( URL u ) throws Exception
{
    URLClassLoader sysLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();

    Class<URLClassLoader> sysclass = URLClassLoader.class;
    Method method = sysclass.getDeclaredMethod("addURL", URL.class);
    method.setAccessible(true);
    method.invoke(sysLoader, new Object[]{u});
}

Step 4. Now you should be able to run your application by calling the application main().

NOTE: This little hack depends on your JVM using URLClassLoader as its SystemClassLoader, which is true for Sun JVMs, not for sure on others.

This way you can deliver a single jar only, and it will unpack itself and run with the correct jars.

轻拂→两袖风尘 2024-08-24 01:20:13

看看这个,有一个代码示例:
创建跨平台java swt应用程序

Look at this, there is a code sample:
Create cross platform java swt application

无声情话 2024-08-24 01:20:13

IIUC,您仍然会遇到指定特定于平台的 JNI 库的问题。您也许可以利用 Java Web Start 为此,但我还没有尝试过。或者,一些项目为支持的平台构建自定义安装程序。例如,在 Mac OS X 上部署 SWT 应用程序描述了如何构建 SWT Mac 应用程序包。此示例中使用了该方法。我还看到过使用这个JarBundler Ant Task

附录:文章在 Java Webstart 上部署 SWT 应用程序 包括一些有用的参考。

IIUC, you'd still have the problem of specifying the platform-specific JNI library. You might be able to leverage Java Web Start for this, but I haven't tried. Alternatively, some projects build custom installers for supported platforms. For example, Deploying SWT Applications on Mac OS X describes how to construct an SWT Mac application bundle. The approach is used in this example. I've also seen this JarBundler Ant Task used.

Addendum: the article Deploying an SWT application on Java Webstart includes some useful references.

悲喜皆因你 2024-08-24 01:20:13

也许 http://one-jar.sourceforge.net/ (Maven 插件位于 http://code.google.com/p/onejar-maven-plugin/)可以朝这个方向提供帮助...

Maybe http://one-jar.sourceforge.net/ (Maven plugin at http://code.google.com/p/onejar-maven-plugin/) could help in that direction...

不奢求什么 2024-08-24 01:20:13

为不同的平台使用不同的 shell 脚本并在脚本中指定特定于平台的 jar 会更容易。

It will be easier to use different shell scripts for different platforms and specify platform-specific jar in the script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文