Monday, April 7, 2008

Deploy simple swt application in eclipse

Once you have your program running in Eclipse, at some point your going to want to share it. It is easy to do - despite all the tutorials about Java Web Start and jnlp .xml files - you don't need a web server. You can build an old fashioned platform specific .jar file very easily.

This tutorial is for SWT v3.3 (and above? I only test on Windows & Linux)

Under your workspace/myprojectname folder create a folder called "build"
under that make a folder for each OS you want to distribute, for example:
/home/clayg/workspace/LeftTabs/build/LeftTabs-linux
/home/clayg/workspace/LeftTabs/build/LeftTabs-winxp

Get your hands on the latest SWT release for the OS you're building for:
Linux - swt-3.3.1.1-gtk-linux-x86.zip
Windows - swt-3.3.1.1-win32-win32-x86.zip

Once you extract that archive you'll see a file called "swt.jar" right in the root - that's your platform specific implementation of all the SWT classes your neat little app is using. It has to go in the working directory of the .jar you're going to create for your application.

So copy the swt.jar from the linux release into:
/home/clayg/workspace/myprojectname/build/myprojectname-linux
and copy the swt.jar from the windows release into:
/home/clayg/workspace/myprojectname/build/myprojectname-winxp

(C:\Documents and Settings\clayg\workspace\myprojectname\build\myprojectname-[platform])

Then go into eclipse. Open your project, we're going to create a manifest for your application's java archive. The manifest tells the java runtime which class to execute and what classes it depends on (I'm pretty sure that's basically what it's doing?).

Right click on your project in the Package Explorer and select New -> File. Name it myprojectname-manifest.txt (LeftTabs-manifest.txt). Fill it with the following goodness:

Manifest-Version: 1.0
Class-Path: swt.jar
Main-Class: myprojectname
[blank line]

Replace myprojectname with the name of the class that contains your main method, which is hopefully also the name of the project, and also more than likely your one and only source file? Replace [blank line] with you guessed - a blank line. I tried it without the blank line, and you really do need it - LAME.

Now right click on your project again, and this time selected Export. In the wizard, under Java - pick "JAR File" and click Next. Your project should already be selected, under "Select the export destination:" click Browse next to "JAR File"

You want to create a file called myprojectname-[platform].jar under the folder:
/home/clayg/workspace/myprojectname/build/myprojectname-[platform]
so the first time I did this for the linux platform, I ended up with
/home/clayg/workspace/LeftTabs/build/LeftTabs-linux/LeftTabs-linux.jar
the second time, when I did this again for windows, I got:
/home/clayg/workspace/LeftTabs/build/LeftTabs-winxp/LeftTabs-winxp.jar

Click Next, make sure "Export class files with warnings" is selected, click Next again.

Now choose "Use existing manifest from workspace" and click Browse. Select the myprojectname-manifest.txt, click OK. Now just hit Finish to build your application.

You need the whole myprojectname-[platform] directory to run your app.
In windows:
double click the myprojectname-winxp.jar file
In linux open a console in the myprojectname-linux folder, and run:
$java -jar myprojectname-linux.jar
from the command prompt

In the process of building my LeftTabs application for windows, I discovered a bug that didn't show up when I was building it under Linux. I believe the end result is actually a more elegant solution.

Line 374:
text.addListener(SWT.FocusOut, new Listener()
{ ...

became:
text.addListener(SWT.Deactivate, new Listener()
{ ...

The purpose of this Listener was to trigger the Modify Event for the view Widget which would tell the main application that the data in the view should be written back to the selected item in the tree. I was doing this when Text item in the view LOST FOCUS. But apparently in windows this happens after the new item is selected in the Tree - which caused the data in the tree to loose sync with the data in the view.

Anyway, vivir es aprender, I didn't know an event SWT.Deactivate existed. Works great.

5 comments:

vignesh said...

I wrote a manifest and built my project for winxp environment. But my app closes as soon as it opens.. I donno wats the problem .. There were no warning generated. I have the Jar in the build folder that i created.

clayg said...

For this specific issue, you may want to check your compiler compliance level.

Window -> Preferences -> Java -> Compiler

But, anytime you run into this kind of issue you should try launching the .jar file from a command prompt. Error message may give you more info.

Anonymous said...

I followed this tutorial on my Win2003 machine. I do not have a one-class application such as in your example. In fact my app has a dense project hierarchy. Nevertheless, I specified the Main-Class entry in POS System-manifest.mf as:
Main-Class: pos_system.MainScreen

Upon launching the executible jar I get an error "could not find the main class: pos_system.MainScreen"
Any ideas?

clayg said...

No, I'm sorry - I don't have any guidance for you on that issue. I'd love to hear about it if you get it fixed!?

Anonymous said...

Here's how I got it to work:
In Eclipse go to file > export > executible jar file. No need to tamper with the Manifest.mf file or any class files. Eclipse will create a perfectly working jar for you.
Important: make sure that any resource files (i.e. images, txt, property files) reside in the same folder directory as your jar file.
You're done!