Build Faster with This Turnkey SWT Project Template-3 : Page 3
by Stephen Strenn
Jan 17, 2007
Page 3 of 3
To Create Executables and/or Installers:
Double-click build.xml.
Modify the execJarBaseName, orgName, and version properties (1, 2, and 4, as shown in Figure 2) as appropriate for your application. The mainClassFullName property (3) should already by correct, but if it is not, modify it now.
Figure 2. Set your properties as needed in the build file.
To build the executable JAR file, go to the Outline view for build.xml. Right-click on buildExecJar, select Run As | Ant Build. Assuming everything goes well, this should put an executable JAR in your project directory. Double-click it in Windows Explorer and verify that your application runs as expected.
To build the Windows .exe file, you must first have Launch4j installed on your machine. The Ant script (build.xml) assumes that launch4j is installed at C:\Program Files\launch4j. If that is not correct, modify the launch4j.dir property in build.xml accordingly. Now, go to the Outline view for build.xml and right-click on buildWinExe, select Run As | Ant Build. Assuming everything goes well, this should put a Windows .exe file in your project directory. Double-click it in Windows Explorer and verify that your application runs as expected.
To build the Windows installer, you must first have NSIS installed on your machine. The Ant script (build.xml) assumes that NSIS is installed at C:\Program Files\NSIS. If that is not correct, modify the makeNsis property in build.xml accordingly. Now, go to the Outline view for build.xml, and right-click on buildWinInstaller, select Run As | Ant Build. Assuming everything goes well, this should put an executable Windows installer in your project directory. Double-click it in Windows Explorer and verify that your application is installed on your computer. The app is available via the Start menu. The application can be uninstalled via the Windows Add/Remove Programs control panel.
Author's Note: buildWinInstaller is dependent on buildWinExe, which is dependent on buildExecJar. Therefore, if you choose to run buildWinInstaller before the executable JAR or the .exe files are created, Ant will automatically build the JAR and .exe for you.
More Customization: Modify images in /src/images, /launch4j, and /nsis as you like to provide an appropriate look and feel for your app. You might find MyPng2Ico useful for converting .png files to .ico files. You should also feel free to modify build.xml, installertemplate.nsi, and licensetemplate.txt however you like. By the way, the text in licensetemplate.txt is a BSD license.
Project Files
Table 1 contains brief descriptions of important files and directories in the template project.
Table 1. Index of Template Contents
Filename
Description
appsettings.ini
Application preferences (Java Properties format).
build.xml
Ant file used to automate the building of executable JARs, .exe's, and installers.
/launch4j
Contains images and icons used by the launch4j native application launcher.
/lib
Contains JAR files used by the application. The template includes swt.jar, which provides access to the SWT widgets.
/nsis
Contains resource files used by NSIS to create a Windows application installer. This includes splash images, an NSIS template script, and a license file.
/src
Contains Java source files and application images
MainWindow.java
Source for the main window and user interface thread
TextFile.java
Utility class for reading and writing text files
SWTResourceManager.java
Utility class used by the Jigloo GUI builder
swt-win32-3235.dll
SWT DLL for Windows
The SWT Project Template is guaranteed to get your small to medium-sized SWT application up and running quickly by providing lots of built-in, time-saving features such as a default user interface, easy text file access, arrow and hourglass cursors, application preferences, and executable and installer build tasks.
So what's next? Download the SWT Project Template and try if for yourself. For example, you can build a simple text editor by dropping an SWT Text widget on the clientArea. Next, use TextFile.read() in openFileMenuItemWidgetSelected() to load the text file that the user selects. Similarly, you can add a saveFileMenuItemWidgetSelected and use TextFile.write() to save changes to the file. Then run the Ant buildWinInstaller task, which will build a single installer file. Finally, copy the installer to another computer, install the application, and then launch it to see just how easy it can be to build distributable SWT applications.
Stephen Strenn obtained his Master's Degree in Electrical Engineering from the University of California at Davis. He is a principal investigator at the Cogito Research Group and a consultant for the infrared detector industry. He has previously published in the areas of evolutionary computation, artificial intelligence, and robotics.