I am using JWebBrowser in a swing application. This class belongs to The DJ Project. It needs swt jar to execute. Now I have included swt jar for windows to my jar packaging of the application. I want to know how can I include swt jars for linux/mac in the same packaging? I am using ant to build the application jar. Should I build the jar putting different swt jar for different platform?
-
A Jar or a java file is supposed to be platform independent. You saying swt.jar is unique for each platform. Me thinks this defeats the whole point of platform independence which is the power of Java. – bragboy Aug 20 '11 at 23:06
-
3possible duplicate of [Does SWT distribute a JAR that works on any supported operating system?](http://stackoverflow.com/questions/976400/does-swt-distribute-a-jar-that-works-on-any-supported-operating-system) – Adam Paynter Aug 20 '11 at 23:53
-
@Bragboy: Good point; on balance, [tag:java-web-start] can mitigate this. – trashgod Aug 21 '11 at 02:41
-
possible duplicate of [Create cross platform Java SWT Application](http://stackoverflow.com/questions/2706222/create-cross-platform-java-swt-application) – Alexey Romanov Aug 22 '11 at 23:22
3 Answers
if you want to have a single build that runs on different platforms (Win/Mac/Linux/*nix) or architectures (32/64 bit) then you can bundle the SWT jar for each target platform with your installer and then load the correct one dynamically at runtime (or have your installer copy the correct SWT jar at installation time).
E.g. say you want to support 32 and 64 bit Windows and Linux you would have SWT jars:
lib/swt_win_32.jar
lib/swt_win_64.jar
lib/swt_linux_32.jar
lib/swt_linux_32.jar
Make your ant script / installer include all of these (they are about 1.6MB each) and then at runtime in your code you can detect the OS and architecture using the Java system properties
System.getProperty("os.name");
System.getProperty("os.arch");
to build the name of the correct jar file.
Loading the jar at runtime can be performed by a custom classloader or by calling the protected method URLClassloader.addURL(URL url)
using reflection.
I've put working code to perform this exact task on my website: http://www.chrisnewland.com/select-correct-swt-jar-for-your-os-and-jvm-at-runtime-191
If you can stand the code-smell then it's a quick solution to a very common SWT problem.

- 630
- 7
- 10
-
1The issue with this approach is that it depends on the JVM the code is ran on. On 64 bits machines, you can run 32 bit JVM and it'll report 32 bit. When you try to load 32 swt jars you'll get a InvocationTargetException because you try to load a 32 bit swt on a 64 bit machine. This only works as long as your JVM matches your computer (i.e. 32 bit JVM on 32 bit computer). I use the code from your link and have issues when users run a 32 bit JVM on a 64 bit machine. Still trying to figure out how to fix that. – javydreamercsw Jan 30 '13 at 17:06
On Mac OS +X, you can incorporate the required JAR and JNI libraries in an application bundle, as shown in this project. See also Deploying SWT Applications on Mac OS X.
On Linux, most platforms make an swt-gtk
package available. As a concrete example, here's a startup script for AppleCommander
:
java -Djava.library.path=/usr/lib/jni \
-cp /usr/lib/java/swt-gtk-3.5.1.jar:AppleCommander-1.3.5.8.jar \
com.webcodepro.applecommander.ui.AppleCommander -swt

- 203,806
- 29
- 246
- 1,045
-
1The problem is solved by following the way described [here](http://stackoverflow.com/questions/2706222/create-cross-platform-java-swt-application/5784073#5784073) – Tapas Bose Aug 22 '11 at 15:52
This answer contains the code to select the correct SWT JAR when you start your application: Create cross platform Java SWT Application
All you need to do is put all the JARs in the correct folder and the code will pick them up.

- 1
- 1

- 321,842
- 108
- 597
- 820