0
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();
                }

    }   
    }

}

This is a small messaging app that I've finished. Everything in here works fine in Eclipse. When I try to run it in the Terminal, however, I get this.

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)

I'm pretty sure this error happens when a trying to access the Display from something that isn't in "main", which isn't what I'm trying to do. So why is it giving me this error?

GamerWiz
  • 3
  • 4
  • @greg-449 Yes! You confirmed what I just kinda learned with a little more research into the problem. Thanks! – GamerWiz Jan 20 '20 at 19:14

1 Answers1

0

Judging by the line numbers in the Display code you are running this on macOS.

On macOS you must specify the -XstartOnFirstThread option when you run your code with the java command in Terminal.

The program works in Eclipse because Eclipse sets this up for you automatically in the Run Configuration.

greg-449
  • 109,219
  • 232
  • 102
  • 145