SWT Exception when running jar: Exception in threa

2019-01-15 16:47发布

问题:

When executing my jar from terminal I am getting the following:

 ***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(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 commonDenom.UserInterface.main(UserInterface.java:26)an error

I've searched for his error and found a few with the same execution output error, but nothing with a solution to my situation.

The following details the locations of files, manifest content, terminal steps taken and finally the code content of the two class files involved.

File Locations

  • SWT library

    /Dropbox/workspace/org.eclipse.swt/swt.jar
    
  • Manifest

    /Dropbox/workspace/commonDenom/bin/Manifest.txt
    
  • Classes

     /Dropbox/workspace/commonDenom/bin/commonDenom/
         commonDenom.class
         UserInterface.class
         UserInterface$1.class (I didn't create this)
         UserInterface$2.class (I didn't create this)
    
  • CommonDenom.jar (see creation below):

    /Dropbox/workspace/commonDenom/bin/CommonDenom.jar
    

Manifest.txt content:

Main-Class: commonDenom.UserInterface
Class-Path:  /Users/skuredjian/Dropbox/workspace/org.eclipse.swt/swt.jar

Terminal actions

  • Directory change

    cd Dropbox/workspace/comonDenom/bin/
    
  • .jar creation

    jar cfm CommonDenom.jar ../Manifest.txt *
    
  • Manifest check

    jar tf CommonDenom.jar
        META-INF/
        META-INF/MANIFEST.MF
        commonDenom/
        commonDenom/commonDenom.class
        commonDenom/UserInterface$1.class
        commonDenom/UserInterface$2.class
        commonDenom/UserInterface.class
        swt.jar
    
  • CommonDenom.jar execution

    java -jar CommonDenom.jar
        ***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(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 commonDenom.UserInterface.main(UserInterface.java:26)
    

Code

UserInterface.class content

package commonDenom;

import java.util.Arrays;
import java.util.Scanner;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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 UserInterface {
    Shell shell;
    Button btnNext;
    Button btnDone;
    Text input;
    Text output;
    static int count;
    static int[] finalNums;
    int[] nums = new int[1000];

    public static void main(String[] args){
        Display display = new Display();
        new UserInterface(display);
        display.dispose();
    }

    public UserInterface(Display display){
        shell = new Shell(display);
        shell.setSize(220,350);
        shell.open();

        input = new Text(shell, SWT.SINGLE);
        input.setBounds(10, 10, 100, 20);

        btnNext = new Button(shell, SWT.PUSH);
        btnNext.setBounds(10, 40, 100, 30);
        btnNext.setText("Next");
        nextPress();

        btnDone = new Button(shell, SWT.PUSH);
        btnDone.setBounds(10, 80, 100, 30);
        btnDone.setText("Done");
        donePress();

        output = new Text(shell, SWT.SINGLE);
        output.setBounds(10, 120, 200, 200);

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

    public void nextPress(){

        btnNext.addSelectionListener(new SelectionAdapter(){
            int x = 0;
            @Override
            public void widgetSelected(SelectionEvent e) {
                    nums[x] = Integer.parseInt(input.getText());
                    System.out.println("nums[" + x + "]:" + nums[x]);
                    x++;
                    count++;
            }
        });
    }

    public void donePress(){
        btnDone.addSelectionListener(new SelectionAdapter(){
            @Override
            public void widgetSelected(SelectionEvent e) {
                finalNums = new int[count]; 
                for(int i = 0; i < count; i++){
                    finalNums[i] = nums[i];
                }
                System.out.println("finalNums:" + Arrays.toString(finalNums));
                commonDenom.compare();
                if(commonDenom.getResult() == 0){
                    output.setText(Arrays.toString(finalNums) + "\nThese numbers do not have a \ncommon multiplier");
                }
                else{
                    output.setText(Arrays.toString(finalNums) + "\nResult:" + String.valueOf(commonDenom.getResult()));
                }
            }
        });
    }
    public static int[] getNums(){
        return finalNums;
    }
}

commonDenom.class content:

package commonDenom;

import java.awt.Button;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import org.eclipse.swt.widgets.*;

public class commonDenom{
    static int result;
    public static void main(String[] args){ 

    }

    public static String compare(){
        result = 0;
        int mult = 0;
        int x = 1;
        int[] nums = UserInterface.getNums();

        // find highest in set
        for(int i  = 0; i < nums.length; i ++){
            if (nums[i] > mult) mult = nums[i];
        }

        // finds lowest common multiple
        for(int i  = 0; i < nums.length;){
            if((mult * x) % nums[i] == 0){
                result = mult * x;
                i++;
            }
            else{
                result = 0;
                x++;
                i = 0;
            }
        }       
    }

    public static int getResult(){
        return result;
    }
}

回答1:

On a Mac you must specify the -XstartOnFirstThread command line option to get SWT to run correctly.



标签: java swt