问题描述
从终端执行我的jar时,我得到以下内容:
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.
以下详细说明了文件,清单内容,termi最后采取的步骤,最后涉及的两个类文件的代码内容。
The following details the locations of files, manifest content, terminal steps taken and finally the code content of the two class files involved.
文件位置
-
SWT library
SWT library
/Dropbox/workspace/org.eclipse.swt/swt.jar
清单
Manifest
/Dropbox/workspace/commonDenom/bin/Manifest.txt
Classes
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(见下面的创建):
CommonDenom.jar (see creation below):
/Dropbox/workspace/commonDenom/bin/CommonDenom.jar
Manifest.txt内容:
Main-Class: commonDenom.UserInterface
Class-Path: /Users/skuredjian/Dropbox/workspace/org.eclipse.swt/swt.jar
终端操作
-
目录更改
Directory change
cd Dropbox/workspace/comonDenom/bin/
.jar creation
.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执行
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)
代码
UserInterface.class内容
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内容:
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;
}
}
推荐答案
开在Mac中,您必须指定 -XstartOnFirstThread
命令行选项才能使SWT正确运行。
On a Mac you must specify the -XstartOnFirstThread
command line option to get SWT to run correctly.
这篇关于运行jar时的SWT异常:线程“main”中的异常org.eclipse.swt.SWTException:无效的线程访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!