运行jar时的SWTexception:线程“main”中的exceptionorg.eclipse.swt.SWTException:无效的线程访问
从终端执行我的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.(Unknown Source) at org.eclipse.swt.widgets.Display.(Unknown Source) at org.eclipse.swt.widgets.Display.(Unknown Source) at commonDenom.UserInterface.main(UserInterface.java:26)an error
我已经搜索了他的错误并发现了一些具有相同的执行输出错误,但没有解决我的情况。
以下详细介绍了文件的位置,清单内容,所采用的终端步骤以及最终涉及的两个类文件的代码内容。
文件位置
-
SWT库
/Dropbox/workspace/org.eclipse.swt/swt.jar
-
表现
/Dropbox/workspace/commonDenom/bin/Manifest.txt
-
类
/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(见下面的创建):
/Dropbox/workspace/commonDenom/bin/CommonDenom.jar
Manifest.txt内容:
Main-Class: commonDenom.UserInterface Class-Path: /Users/skuredjian/Dropbox/workspace/org.eclipse.swt/swt.jar
终端行动
-
目录更改
cd Dropbox/workspace/comonDenom/bin/
-
.jar创作
jar cfm CommonDenom.jar ../Manifest.txt *
-
清单检查
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执行
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.(Unknown Source) at org.eclipse.swt.widgets.Display.(Unknown Source) at org.eclipse.swt.widgets.Display.(Unknown Source) at commonDenom.UserInterface.main(UserInterface.java:26)
码
UserInterface.class内容
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内容:
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 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; } }
在Mac上,您必须指定-XstartOnFirstThread
命令行选项以使SWT正确运行。