java 如何使用在所有平台上运行的 SWT 创建可执行 JAR?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2037220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I creating executable JAR with SWT that runs on all platforms?
提问by Aaron Digulla
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?
SWT 带有一个基本 JAR 和每个平台(Windows、Linux/32 位、Linux/64 位、Mac、AIX 等)一个特定的 JAR。如何创建一个可执行的 JAR,以便在运行时选择正确的平台 JAR?
[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?
[编辑] 我想在一个子目录中提供所有平台 JAR,main()然后将修改类加载器。有没有人已经尝试过这个?
采纳答案by Matías
Look at this, there is a code sample: Create cross platform java swt application
看这个,有一个代码示例: Create cross platform java swt application
回答by karoberts
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().
对于我当前的工作,我需要提供一个可执行的 jar,它可以在自身内部加载 jar 并执行第二个 main()。基本上是一个引导程序 main() 和一个应用程序 main()。
Step 1. in the manifest "main-class" you put your bootstrap class
第 1 步。在清单“主类”中,您将引导类放入
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.
第 2 步。当您的引导程序类运行它时,它会解压它自己的 jar 并将其中的所有 jar 放到一个临时目录中。使用类似下面的代码来获取你自己的 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
第 3 步。您的引导程序类通过“os.name”属性检测操作系统,并使用此属性从临时目录加载适当的 jar
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().
第 4 步。现在您应该能够通过调用应用程序 main() 来运行您的应用程序。
NOTE: This little hack depends on your JVM using URLClassLoaderas its SystemClassLoader, which is true for Sun JVMs, not for sure on others.
注意:这个小技巧取决于你的 JVMURLClassLoader用作它的 SystemClassLoader,这对于 Sun JVM 来说是正确的,在其他人上不确定。
This way you can deliver a single jar only, and it will unpack itself and run with the correct jars.
通过这种方式,您只能交付一个 jar,它会自行解压并使用正确的 jar 运行。
回答by trashgod
IIUC, you'd still have the problem of specifying the platform-specific JNI library. You might be able to leverage Java Web Startfor this, but I haven't tried. Alternatively, some projects build custom installers for supported platforms. For example, Deploying SWT Applications on Mac OS Xdescribes how to construct an SWT Mac application bundle. The approach is used in this example. I've also seen this JarBundler Ant Taskused.
IIUC,您仍然会遇到指定特定于平台的 JNI 库的问题。您也许可以利用Java Web Start来实现这一点,但我还没有尝试过。或者,一些项目为支持的平台构建自定义安装程序。例如,在 Mac OS X 上部署 SWT 应用程序描述了如何构建 SWT Mac 应用程序包。本例中使用了该方法。我还看到了这个JarBundler Ant Task 的使用。
Addendum: the article Deploying an SWT application on Java Webstartincludes some useful references.
附录:在 Java Webstart 上部署 SWT 应用程序一文包含一些有用的参考资料。
回答by Denis Tulskiy
It will be easier to use different shell scripts for different platforms and specify platform-specific jar in the script.
对于不同的平台使用不同的 shell 脚本并在脚本中指定特定于平台的 jar 会更容易。
回答by Marcel St?r
Maybe http://one-jar.sourceforge.net/(Maven plugin at http://code.google.com/p/onejar-maven-plugin/) could help in that direction...
也许http://one-jar.sourceforge.net/(Maven插件在http://code.google.com/p/onejar-maven-plugin/)可以帮助在这个方向......

