JavaSWT应用程序可以在Eclipse中运行,但不能在终端中运行

问题描述 投票:0回答:1
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class clientWindow {

    static Text chatWindow;

    public static void sendMessage(Socket socket, String message) throws IOException {
        PrintWriter pr = new PrintWriter(socket.getOutputStream());
        pr.println("Client: " + message);
        pr.flush();
    }

    public static void main(String[] args) throws UnknownHostException, IOException {
        Socket s = new Socket("10.0.1.8", 4500);
        Display display = new Display();
        Shell clientWindow = new Shell(display);
        GridLayout layout = new GridLayout();
        layout.numColumns = 1;
        clientWindow.setLayout(layout);

        GridData data = new GridData(GridData.FILL_HORIZONTAL);
        GridData data1 = new GridData(GridData.FILL_BOTH);

        chatWindow = new Text(clientWindow, SWT.MULTI | SWT.V_SCROLL | SWT.READ_ONLY);
        chatWindow.setLayoutData(data1);
        Text messageBox = new Text(clientWindow, SWT.SINGLE);
        messageBox.setLayoutData(data);
        Button send = new Button(clientWindow, 0);
        send.setText("Send");
        send.addSelectionListener(new SelectionListener() {
            public void widgetSelected(SelectionEvent event) {
                try {
                    sendMessage(s, messageBox.getText());
                    messageBox.setText("");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent arg0) {
                // TODO Auto-generated method stub

            }
        });

        clientWindow.open();

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

    }   
    }

}

这是我已经完成的小型消息传递应用程序。此处的所有内容在Eclipse中均可正常运行。但是,当我尝试在终端中运行它时,我得到了。

Exception in thread "main" org.eclipse.swt.SWTException: Invalid thread access
    at org.eclipse.swt.SWT.error(SWT.java:4711)
    at org.eclipse.swt.SWT.error(SWT.java:4626)
    at org.eclipse.swt.SWT.error(SWT.java:4597)
    at org.eclipse.swt.widgets.Display.error(Display.java:1112)
    at org.eclipse.swt.widgets.Display.createDisplay(Display.java:853)
    at org.eclipse.swt.widgets.Display.create(Display.java:837)
    at org.eclipse.swt.graphics.Device.<init>(Device.java:132)
    at org.eclipse.swt.widgets.Display.<init>(Display.java:736)
    at org.eclipse.swt.widgets.Display.<init>(Display.java:727)
    at clientWindow.main(clientWindow.java:28)

我很确定当尝试从不在“ main”中的内容访问Display时会发生此错误,这不是我要执行的操作。那为什么给我这个错误呢?

java eclipse swt
1个回答
0
投票

根据Display代码中的行号判断,您正在macOS上运行此代码。

在macOS上,当您在终端中使用-XstartOnFirstThread命令运行代码时,必须指定java选项。

该程序在Eclipse中有效,因为Eclipse在运行配置中自动为您设置了该程序。

© www.soinside.com 2019 - 2024. All rights reserved.