SWTLoader: run SWT programs with a double click, v.1.01
Introduction
SWT and
SwingWT-based programs, as well as all other Java programs with native libraries, are often tricky to
deploy.
Because the native libraries are needed, the users have either to install those in a directory in the PATH (assuming that they know what it is) or
a platform-specific installer or script must be deployed.
Jar files would be preferrable, because a double-click would launch them on most platforms, but the Java Virtual Machine cannot use native
libraries in Jars, in facts you'll have to set the -Djava.library.path commandline option in most cases.
SWTLoader is an elegant hack to avoid all this, allowing you to deploy self-contained, self-extracting native libraries such as
SWT in a single Jar file.
How to use it
You can use SWTLoader with three easy steps:
-
import net.moioli.swtloader.*; in the class with the main() method;
- Create a new SWTLoader as the first instruction in the main method:
public static void main(String[] args){
new SWTLoader("packagename.jar");
...
- Include in the Jar file the needed .dll, .so or .jnilib files. Ant example:
<target name="demo-SwingWT-linux" >
<javac srcdir="${srcDir}" destdir="${buildDir}">
<classpath>
<pathelement path="swt.jar"/>
<pathelement path="swt-pi.jar"/>
</classpath>
<include name="**/*.java" />
</javac>
<unjar src="$swt.jar" dest="${buildDir}"/>
<unjar src="$swt-pi.jar" dest="${buildDir}"/>
<copy todir="${buildDir}">
<fileset dir="${swtLinuxLibDir}/">
<include name="*.so"/>
</fileset>
</copy>
<jar destfile="${buildDir}/demo-SwingWT-linux.jar" basedir="${buildDir}">
<fileset dir="${buildDir}" >
<include name="**/*.class" />
<include name="**/*.so"/>
</fileset>
<manifest>
<attribute name="Main-Class" value="SWTDemo"/>
</manifest>
</jar>
</target>
Users, double-clicking or using the command java -jar packagename.jar will get a fully functional
SWT-enabled JVM.
How does it work?
The inner workings of the script are quite easy: first, it creates a temporary directory, then it unpacks the needed files and finally it
re-invokes the JVM with the needed -D option. Note that you'll have
only one JVM running at a time: the first VM instance will be
terminated safely.
Also, if
SWT is already loadable, none of these actions will
be performed.
Get it
Here! (sources only)
License
SWTLoader is published under the
GPL license. If you need a non-GPL'd version (eg. for
commercial purposes) email me: silvio at moioli dot net
Feedback
Feedback (both positive and negative) is always welcome. Please write me at: silvio at moioli dot net
Italian-speaking people can also visit
my site.