Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 views
in Technique[技术] by (71.8m points)

java - SWT jar for different platform

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...