SWT : Software Widget Toolkit for Eclipse

SWT is a windowing toolkit initially developed by IBM that many feel has the ability to contend with Swing, core Java’s main windowing toolkit contender.
Starting a Swing application project is as easy as it gets from a configuration perspective, simply because Swing is included with Java. SWT, however, has two additional hurdles: it is an external API and it includes native code.

This tip discusses setting up an Eclipse project that can use the SWT libraries so you can run and debug SWT code inside of your Eclipse environment. I am specifically focusing on Windows because that is my current setup, but the rules for setting up your environment should be similar irrespective of the platform.

In addition, the general steps for configuring SWT with your application should apply in any development environment, I simply use Eclipse in this example.

To set up an SWT application you essentially need two things. You need the SWT Jar to be in the classpath, and you need the SWT dll to be in the library path. You can get these two items in one of two places.

First, they come shipped with your Eclipse installation. You can find them in the plugins directory. For the JAR, simply look into [Eclipse Home]/plugins/org.eclipse.swt_ – where version number is your Eclipse installation’s version (e.g. 3.1.0). For the DLL (on Windows), simply look into [Eclipse Home]/plugins/org.eclipse.swt.win32_ .

Second, they are also available on the Eclipse website for download. Rather than provide a link that will quickly become outdated however, I will describe how to get there. Simply start at http://www.eclipse.org/downloads/index.php

, choose a mirror, choose an Eclipse version you would like the SWT build for, and then scroll down the Eclipse version download page to find the SWT Binaries and Sources downloads.
Once you have these two resources, it is simply a matter of creating an Eclipse project, and referring to these resources as neccessary.

Creating a Java project is relatively simple in Eclipse, and I won’t go into the process in detail here. If you are interested, the tips for that, along with a lot of other Java development features can be found in ‘Help -> Tips and Tricks -> Eclipse Java Development Tools’.

Assuming you have a Java project for your SWT application, the first step is to refer to the SWT jar. I personally like to copy all neccessary resources in to my project (e.g. a ‘lib’ folder), and then refer to them as neccessary. This ensures that each project has the correct dependencies associated with it. Once again, I don’t plan to go into detail here regarding the association of JAR files with an Eclipse project – the Eclipse ‘Help’ documentation should be helpful if you aren’t familiar. Here is an example setup.


The SWT Jar is handled in the same way as any other external JAR, so more than likely the advice up to this point has been ‘run of the mill’ for many Eclipse users. The DLL file, on the other hand, is unusual for most developers. So, how do we set it up?

From the SWT download page:

To run a standalone SWT application, add the swt jar(s) to the classpath and add the directory/folder for the SWT JNI library to the java.library.path. For example, if you extract the download below to C:\SWT you would launch the HelloWorld application with the following command:
java -classpath C:\SWT\swt.jar;C:\MyApp\helloworld.jar -Djava.library.path=C:\SWT


By handling the JAR file we’ve handled the ‘-classpath’ part of the configuration above, but how do we configure a library path in Eclipse? It turns out this is simply part of the Eclipse runtime configuration for our application. For reference, a runtime configuration is the ‘Run’ menu entry for starting our project. Once again I point you to the ‘Help’ documentation if you aren’t familiar with runtime configurations.
Assuming we create our new ‘main method’ class, and call it ‘HelloWorld’, we can then set up a runtime configuration with this special line in the VM Arguments field:


Note the use of ‘project_loc’ – this allows us to make the VM argument relative to our project. This certainly isn’t a neccessary step (we could have easily placed ‘c:\SWT’ in this location as well), but it can take great strides in making your project setup more portable to future changes. Also note that by doing this I have been able to bundle the DLL and JAR in the same general location. This is especially important because each SWT Jar has a unique SWT DLL associated with it – they can’t be interchanged. Having the two bundled in a portable location in the project ensures that they won’t get out of sync.

Now it is just a matter of placing some SWT test code in our ‘HelloWorld’ class. From the SWT Component Resources page (http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html),

here is the complete Java code for SWT ‘Hello World’, adapted to meet our class name:

import org.eclipse.swt.widgets.*;   public class HelloWorld {   public static void main (String [] args) { Display display = new Display (); Shell shell = new Shell(display); shell.open (); while (!shell.isDisposed ()) { if (!display.readAndDispatch ()) display.sleep (); } display.dispose (); } }

 java Tutorials by Ashutosh Sharma, pls Visit:



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: