I am trying to get a hello world SWT application going:
public static void main(String args[]) throws IOException{
Display display = new Display ();
Shell shell = new Shell(display);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
But I keep getting the following exception. I have the macosx version of org.eclipse.swt.carbon being used with eclipse/maven. Has anyone seen this before?
Exception in thread "main" java.lang.UnsatisfiedLinkError: no swt-carbon-3346 or swt-carbon in swt.library.path, java.library.path or the jar file
at org.eclipse.swt.internal.Library.loadLibrary(Library.java:219)
at org.eclipse.swt.internal.Library.loadLibrary(Library.java:151)
at org.eclipse.swt.internal.C.<clinit>(C.java:21)
at org.eclipse.swt.widgets.Display.createDisplay(Display.java:943)
at org.eclipse.swt.widgets.Display.create(Display.java:937)
at org.eclipse.swt.graphics.Device.<init>(Device.java:119)
at org.eclipse.swt.widgets.Display.<init>(Display.java:749)
at org.eclipse.swt.widgets.Display.<init>(Display.java:740)
at com.wuntee.aat.command.adb.LogCat.main(LogCat.java:30)
SWT is implemented using native UI libraries – in this case, the Mac Carbon libraries – and requires some JNI code to marshal the SWT calls to the native Carbon calls. Loading JNI libraries is a little bit different than normal classloading (the dynamic libraries must first be loaded with
System#loadLibrary).First, if you’re on a 64 bit machine, make sure that you’re passing the
-d32argument to the JRE. (ie,java -d32 MyTestClass). The Carbon SWT libraries are 32 bit only and will not load on a 64 bit runtime.By default, SWT tries to load the
.jnilibdynamic libraries from a JAR file in your classpath – the librarylibswt-carbon-3346.jnilibshould be in yourorg.eclipse.swt.carbon.macosx...JAR. SWT will try to unzip thisjnilibfrom your JAR into a temporary location and load that.If it can’t do this for whatever reason (
java.io.tmpdirisn’t writable, for example) then this will fail. To work around this, you should extractlibswt-carbon-3346.jnilibfrom theorg.eclipse.swt.carbon.macosxJAR and follow the instructions that the exception provided. (For example, setswt.library.pathto the location of that jnilib.)Unrelated to your actual question: you’re using SWT 3.3. A lot of changes have been made since then, including the (likely preferable) Cocoa support. Upgrading to the newest SWT libraries in the newest Eclipse may be very helpful.