SWT window 可在 Linux 上运行但不能在 Mac 上运行

SWT window runnable on Linux but not on Mac

我在我的项目中添加了一个 SWT window

public class swtUI {
    protected Shell shell;
    protected Display display;

    public static void launch() {
        try {
            swtUI window = new swtUI();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void open() {
        display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    /**
     * Create contents of the window.
     */
    protected void createContents() {
        shell = new Shell();

        // create all controls
        blablabal
    }
}

并在 main() 中将其称为

public static void main(String[] args) {
    swtUI.launch();
    System.out.println("\nComplete");   
}

没问题 运行宁 Linux。 但是当我在 mac 上用 maxos swt.jar 和 运行 替换 linux swt.jar 时,我得到了异常:

    ***WARNING: Display must be created on main thread due to Cocoa restrictions.
org.eclipse.swt.SWTException: Invalid thread access
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.widgets.Display.error(Unknown Source)
    at org.eclipse.swt.widgets.Display.createDisplay(Unknown Source)
    at org.eclipse.swt.widgets.Display.create(Unknown Source)
    at org.eclipse.swt.graphics.Device.<init>(Unknown Source)
    at org.eclipse.swt.widgets.Display.<init>(Unknown Source)
    at org.eclipse.swt.widgets.Display.<init>(Unknown Source)
    at org.eclipse.swt.widgets.Display.getDefault(Unknown Source)
    at edu.rockefeller.casanovaLab.csvCheck.CsvSuiteGUI.open(CsvSuiteGUI.java:129)
    at edu.rockefeller.casanovaLab.csvCheck.CsvSuiteGUI.launch(CsvSuiteGUI.java:119)
    at edu.rockefeller.casanovaLab.csvCheck.Program.main(Program.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)

我对 SWT 还很陌生。我应该如何更改我的代码以使其在 mac 上运行?

谢谢

this FAQ 中似乎提到了这一点。来自同一个 link:

To work around this problem you'll have to pass the -XstartOnFirstThread option to the java executable as follows:

java -XstartOnFirstThread -cp swt.jar:. ControlExample