Things I wish I knew when I first started using SWT on a Mac
I've got a friend who I persuaded to try Eclipse and is just learning how to use it. So it got me thinking: what would I have wanted someone to tell me when I first started programming with Eclipse? Today, I'll focus on running SWT apps on a Mac. Why pick that? Well, because creating "Hello World" in Eclipse on any platform is very simple.
SWT on the other hand, presents an interesting initial hurdle, but is straightforward after that first bump. The reason why is because, unlike Swing, SWT relies on native libraries to achieve a native, fast, and responsive look and feel on each platform.
OK, so hopefully you've gotten to the point where you have some SWT code that has some red Xes which indicate that "Display" and "Shell" cannot be found. Here's some example code, equivalent to an "SWT Hello World": (Note that this needs to be in a package named "com.luisdelarosa.swtdemo".
package com.luisdelarosa.swtdemo;import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell;
/**
- @author luis
- Hello World in SWT. */ public class SWTHelloWorld { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout()); Label label = new Label(shell, SWT.CENTER); label.setText("Hello, world!"); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }
To correct the red Xes, there's many ways to do it. Today, I'll show you the easy, but somewhat repetitive way.
- Choose "Project" | "Properties" from the main menu.
- Navigate to the "Java Build Path" list item, then to the "Libraries" tab.
- Press the "Add External JARS..." button. This will open up the "JAR Selection" dialog.
- Navigate down (with ECLIPSE_HOME being you Eclipse install directory) to: ECLIPSE_HOME/plugins/org.eclipse.swt.carbon_3.0.1/ws/carbon
- Select both "swt.jar" and "swt-pi.jar".
This is what the JAR Selection dialog should look like at this point. (Click on the thumbnail to see the full size image.)
- Press the "OK" button.
This is what the Project Properties dialog should look like at this point.
- Press the "OK" button.
Then Option-Click (or right-click) on the SWTHelloWorld class and choose "Run" > "Java Application".
At this point, the console will come up, and you'll see a strange looking stack trace in red that says:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no swt-pi-carbon-3063 in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1491) at java.lang.Runtime.loadLibrary0(Runtime.java:788) at java.lang.System.loadLibrary(System.java:834) at org.eclipse.swt.internal.Library.loadLibrary(Library.java:100) at org.eclipse.swt.internal.carbon.OS.(OS.java:22) at org.eclipse.swt.widgets.Display.(Display.java:311) at com.luisdelarosa.swtdemo.SWTHelloWorld.main(SWTHelloWorld.java:19)
Stay calm. Although the stack trace may look a little scary, there is a simple explanation for it: SWT relies on native libraries. It uses these native libraries + JNI + Java classes to produce that nice native, fast, and responsive look and feel that we've come to love in Eclipse. On the Mac, these are in native libraries take the form of ".jnilib" files. There are three of them in Eclipse 3.0.1:
- libswt-carbon-3063.jnilib
- libswt-pi-carbon-3063.jnilib
- libswt-webkit-carbon-3063.jnilib - This enables you to use WebKit, which is the OS X component that powers Safari, inside your SWT apps.
You're only a few steps away from success:
- While the SWTHelloWorld class is selected, choose "Run" | "Run...".
- That should bring up the Run dialog, which has launch configurations, with the SWTHelloWorld configuration selected.
- Navigate to the Arguments tab.
- Enter into the "VM arguments:" text box (No need for substitution because it uses a special variable):
-Djava.library.path=${system:ECLIPSE_HOME}/plugins/org.eclipse.swt.carbon_3.0.1/os/macosx/ppc
- This is what the Run dialog should look like at this point:
- Press the "Run" button.
- You should get a nice little window that says "Hello World" and a new Java icon in your Dock.
Why is this repetitive? Well, you need to repeat the first step of instructions for every Java Project that uses SWT. You need to repeat the last step of instructions for every Java class that uses SWT which you want to run.
Eclipse 3.0.0 notes: If you're still using Eclipse 3.0 on a Mac, then upgrade to 3.0.1! Now! Seriously, if you're in the middle of a development cycle with Eclipse 3.0, just substitute "org.eclipse.swt.carbon_3.0.0" wherever you see "org.eclipse.swt.carbon_3.0.1".
Coming tomorrow: Things I wish I knew when I first started using SWT on Windows. I'm a multi-platform kind of guy. And after that, I'll talk about SWT on Linux.