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.

  1. Choose “Project” | “Properties” from the main menu.
  2. Navigate to the “Java Build Path” list item, then to the “Libraries” tab.
  3. Press the “Add External JARS…” button. This will open up the “JAR Selection” dialog.
  4. Navigate down (with ECLIPSE_HOME being you Eclipse install directory) to: ECLIPSE_HOME/plugins/org.eclipse.swt.carbon_3.0.1/ws/carbon
  5. Select both “swt.jar” and “swt-pi.jar”.
  6. This is what the JAR Selection dialog should look like at this point. (Click on the thumbnail to see the full size image.)

    JAR Selection dialog

  7. Press the “OK” button.
  8. This is what the Project Properties dialog should look like at this point.

    Project Properties dialog

  9. 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:

  1. libswt-carbon-3063.jnilib
  2. libswt-pi-carbon-3063.jnilib
  3. 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:

  1. While the SWTHelloWorld class is selected, choose “Run” | “Run…”.
  2. That should bring up the Run dialog, which has launch configurations, with the SWTHelloWorld configuration selected.
  3. Navigate to the Arguments tab.
  4. 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
  5. This is what the Run dialog should look like at this point:
    Run dialog
  6. Press the “Run” button.
  7. 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.

October 28, 2004. Eclipse. 7 Comments.

7 Comments

  1. Joseph Ottinger replied:

    Don’t forget SWT on Solaris! And HP/UX, too.

    October 29th, 2004 at 10:52 am. Permalink.

  2. Jonathan Martin replied:

    Thanks for the info. I was stuck on the jnilibs part and found this via Google.

    A warning: It still wasn’t working for me, and after a half hour messing around, got it to work. I’m pretty sure it had something to do with the fact that I had my eclipse in the following path: “~/Applications/ =Development/eclipse” (Yes that’s a space-equal sign. don’t ask. Ok, ask. I did that to force the sort order in my Application folder. Pain in the butt, but I’m weird like that). As soon as I moved eclipse to ~, it worked fine. Wrapping the -Djava.library.path… in quotes probably would’ve worked too…

    November 23rd, 2004 at 1:01 pm. Permalink.

  3. Otfried Geffert replied:

    I got the Eclipse-SWT stuff running on MacOSX after reading your page. On windows there is a similar problem, which was solved by copying two swt-*.dll files to \Windows\system32 directory. I am working on several operating systems (Windows/OS-X and sometimes on FreeBSD & Linux).

    Thanks for great help!

    Otfried

    May 15th, 2005 at 3:04 pm. Permalink.

  4. Frank replied:

    Nice article!! The screenshot-links are broken though…

    May 19th, 2005 at 11:21 am. Permalink.

  5. Snow replied:

    Oh boy, I can stop banging my head against the wall. Thanks a lot, I was ready to jump out of the window thanks to the .jnilib stuff.

    Why the eclipse guys don’t insert it as a default is beyond me, though…

    May 23rd, 2005 at 4:40 pm. Permalink.

  6. Markus Sandy replied:

    Hi. I am trying to use eclipse on mac os x tiger. Am familiar with this situation and have resolved with -Djava.library.path before. However, I am using 3138 and there does not seem to be a folder named

    ${system:ECLIPSE_HOME}/plugins/org.eclipse.swt.carbon_3.0.1/os/macosx/ppc

    I did find the three .jnilib files inside a jar:

    ${system:ECLIPSE_HOME}/plugins/org.eclipse.swt_3.1.0.jar

    Did the eclipse install fail to create and extract the appropriate files and folders or have chings changed in a recent version?

    Thanks for documenting this.

    August 10th, 2005 at 6:03 pm. Permalink.

  7. Markus Sandy replied:

    I just discovered the “Run as SWT Application” menu item which seems to launch my sample test app ok. Did not use any setting for java.librar.path.

    Ok for running inside eclipse, but still not sure how this effects command line.

    August 10th, 2005 at 6:29 pm. Permalink.

Leave a Reply