FAQ How do I create an executable JAR file for a stand-alone SWT program?
Creating an executable JAR file for a stand-alone SWT program is very similar to creating any executable JAR file, with one important difference. Starting with Eclipse version 3.3, the file "swt.jar" file includes platform-specific executable files that are unpacked automatically when your SWT program is executed. This means that a given runtime folder will normally be platform specific.
The detailed instructions for creating an executable JAR file for a stand-alone SWT file are listed below.
- Create a runtime folder for the desired runtime target on your system (e.g., c:\swt\runtime-linux). Note that the target platform does not need to be the same as your development platform.
- Find the correct SWT JAR file for the desired target platform. You can download the desired ZIP file from the SWT website. For example, for Eclipse 3.3 and a target platform of Linux, download the file swt-22.214.171.124-gtk-linux-x86.zip. Expand this ZIP file and copy the swt.jar file to the runtime folder. Remember that this swt.jar file is specific to one platform, in this case Linux.
- Create a manifest file for your application using the Eclipse text editor (e.g., myapplication-manifest.txt). The text of the manifest should be as follows:
Manifest-Version: 1.0 Class-Path: swt.jar Main-Class: mypackage.MyClassWithMainMethod (blank line at end of file)
- Make sure the manifest file ends with a blank line. Put the name of your package and class that contains the main() method for the Main-Class.
- In Eclipse, select File/Export/Java/Jar file and press Next.
- On the JAR File Specification dialog, select the source files for the classes you want in the application. In the export destination, browse to the runtime folder and enter in the desired name of the JAR file (e.g., myapplication.jar or myapplication_linux.jar). Press Next.
- On the JAR Packaging Options dialog, make sure the "Export class files with compile warnings" box is checked. Otherwise, if your source files have any compile warnings, they will not be included in the JAR file. Press Next.
- In the JAR Export dialog, select the option "Use existing manifest from workspace". Browse to the manifest file you created above. Press Finish.
- If the JAR file already exists, you will be asked to overwrite it. Select Yes. If your project had any compile warnings, a message will display. If so, press OK.
- At this point, the JAR file for your application has been created in the runtime directory.
- If needed (i.e., your target platform is different than your development platform), copy the runtime directory to a directory on your target platform.
- In your operating system's file explorer, browse to the runtime directory and run your JAR file. For example, in Windows, you can just double-click on it in the Windows File Explorer or, from the "cmd" prompt, you can enter the command: java -jar myapplication.jar. The application should run.
A common error message when trying to run a stand-alone application is: "Could not find the main class. Program will exit.", This error can be caused by any of the following problems:
- The swt.jar file name is missing or misspelled in the mainfest file.
- The swt.jar file is missing from the runtime folder.
- The Main-Class: is incorrect in the manifest file.