How can I creating executable JAR with SWT that ru

2020-02-04 06:03发布

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?

5条回答
Juvenile、少年°
2楼-- · 2020-02-04 06:35

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

查看更多
做个烂人
3楼-- · 2020-02-04 06:37

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.

查看更多
我只想做你的唯一
4楼-- · 2020-02-04 06:47

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

查看更多
放我归山
5楼-- · 2020-02-04 06:51

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.

查看更多
▲ chillily
6楼-- · 2020-02-04 06:55

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

查看更多
登录 后发表回答