Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWT Invalid Thread Access on Mac OSX (Eclipse Helios)

Tags:

java

eclipse

swt

I have the simplest of all simple SWT programs (it doesn't even display hello world yet):

package com.samples.swt.first;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Main {
    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();
    }
}

When I run this on Mac OSX from Eclipse Helios, I get the following error:

***WARNING: Display must be created on main thread due to Cocoa restrictions.
Exception in thread "main" org.eclipse.swt.SWTException: Invalid thread access at org.eclipse.swt.SWT.error(SWT.java:4282) at org.eclipse.swt.SWT.error(SWT.java:4197) at org.eclipse.swt.SWT.error(SWT.java:4168) at org.eclipse.swt.widgets.Display.error(Display.java:1065) at org.eclipse.swt.widgets.Display.createDisplay(Display.java:822) at org.eclipse.swt.widgets.Display.create(Display.java:805) at org.eclipse.swt.graphics.Device.(Device.java:130) at org.eclipse.swt.widgets.Display.(Display.java:696) at org.eclipse.swt.widgets.Display.(Display.java:687) at com.samples.swt.first.Main.main(Main.java:8)

As far as I can tell, I am doing everything correctly. Why am I getting this error? It says that Display must be created on the main thread, and as far as I can tell it is being created on the main thread. It then goes on to talk about Exception in thread "main"...

EDIT:

The error is gone now, I switched from using swt-debug.jar to swt.jar. If anybody knows why the debug jar causes this error I would love to know...

like image 629
Marvin Holt Avatar asked Oct 26 '11 14:10

Marvin Holt


1 Answers

You need to have the -XstartOnFirstThread switch when starting the application. This question on the SWT FAQ explains the reasons.

like image 93
Gorkem Ercan Avatar answered Oct 15 '22 16:10

Gorkem Ercan