使用 SWT 解决 NoClassDefFoundError 异常 [英] Solving NoClassDefFoundError exception with SWT

查看:40
本文介绍了使用 SWT 解决 NoClassDefFoundError 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SWT 库创建程序,但是当我尝试运行它时,出现以下异常:

I am trying to create a program using the SWT library, but when I try to run it I get the following exception:

线程main"中的异常java.lang.NoClassDefFoundError:org/eclipse/swt/widgets/Layout在 il.ac.tau.cs.sw1.trivia.TriviaMain.main(TriviaMain.java:9)引起:java.lang.ClassNotFoundException:org.eclipse.swt.widgets.Layout在 java.net.URLClassLoader.findClass(Unknown Source)在 java.lang.ClassLoader.loadClass(来源不明)在 sun.misc.Launcher$AppClassLoader.loadClass(来源不明)在 java.lang.ClassLoader.loadClass(来源不明)... 1个

Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Layout at il.ac.tau.cs.sw1.trivia.TriviaMain.main(TriviaMain.java:9) Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Layout at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 1 more

它甚至没有开始运行程序,它只是立即崩溃.可以做些什么来解决它?

It doesn't even start to run the program, it just crashes instantly. What can be done to solve it?

我已经添加了我的代码:

I have added my code:

import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;



public class TriviaGUI {

private static final int MAX_ERRORS = 3;
private Shell shell;
private Label scoreLabel;
private Composite questionPanel;
private Label startupMessageLabel;
private Font boldFont;
private String lastAnswer;

// Currently visible UI elements.
Label instructionLabel;
Label questionLabel;
private List<Button> answerButtons = new LinkedList<>();
private Button passButton;
private Button fiftyFiftyButton;

//other
private String path="";
private List<String> currentAnswers;
private SortedMap<String,List<String>> fullQuestion = new TreeMap<>();
private Map<String,String> questionPlusAnswer = new HashMap<>();
private ArrayList<String> numberedQuestions = new ArrayList<>();
private int score;
private int wrongAnswers;
private int numOfQuestions;
private boolean usedFiftyFifty = false;
private boolean usedPass = false;

public void open() {
    createShell();
    runApplication();
}

/**
 * Creates the widgets of the application main window
 */
private void createShell() {
    Display display = Display.getDefault();
    shell = new Shell(display);
    shell.setText("Trivia");

    // window style
    Rectangle monitor_bounds = shell.getMonitor().getBounds();
    shell.setSize(new Point(monitor_bounds.width / 3,
            monitor_bounds.height / 4));
    shell.setLayout(new GridLayout());

    FontData fontData = new FontData();
    fontData.setStyle(SWT.BOLD);
    boldFont = new Font(shell.getDisplay(), fontData);

    // create window panels
    createFileLoadingPanel();
    createScorePanel();
    createQuestionPanel();
}

/**
 * Creates the widgets of the form for trivia file selection
 */
private void createFileLoadingPanel() {
    final Composite fileSelection = new Composite(shell, SWT.NULL);
    fileSelection.setLayoutData(GUIUtils.createFillGridData(1));
    fileSelection.setLayout(new GridLayout(4, false));

    final Label label = new Label(fileSelection, SWT.NONE);
    label.setText("Enter trivia file path: ");

    // text field to enter the file path
    final Text filePathField = new Text(fileSelection, SWT.SINGLE
            | SWT.BORDER);
    filePathField.setLayoutData(GUIUtils.createFillGridData(1));

    // "Browse" button
    final Button browseButton = new Button(fileSelection, SWT.PUSH);
    browseButton.setText("Browse");

    browseButton.addSelectionListener(new SelectionListener() {
        public void widgetSelected(SelectionEvent e) {
                path = GUIUtils.getFilePathFromFileDialog(shell);
                if(path!=null)
                    filePathField.setText(path);
        }

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

            }
    });

    // "Play!" button
    final Button playButton = new Button(fileSelection, SWT.PUSH);
    playButton.setText("Play!");

    playButton.addSelectionListener(new SelectionListener() {
        public void widgetSelected(SelectionEvent e) {
            if(!filePathField.getText().equals(path))
                path = filePathField.getText(); 
            try {
                BufferedReader br = new BufferedReader(new FileReader(new File(path)));
                score = 0;
                scoreLabel.setText(String.valueOf(score));
                wrongAnswers = 0;
                for(String str = br.readLine(); str!=null; str = br.readLine()) {
                    String[] arr = str.split("\t");
                    numberedQuestions.add(arr[0]);
                    questionPlusAnswer.put(arr[0],  arr[1]);
                    List<String> anwersForThisQuestion = new ArrayList<>();
                    for(int i=1; i<str.length(); i++) {
                        anwersForThisQuestion.add(arr[i]);
                    }
                    fullQuestion.put(arr[0], anwersForThisQuestion);
                    updateQuestionPanel(arr[0], anwersForThisQuestion);
                }
                startPlaying();
                br.close();
            } 
            catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                GUIUtils.showErrorDialog(shell, "File " + path + " Not Found");
                e1.printStackTrace();
            } 
            catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }

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

            }
    });
}

protected void startPlaying() {
    numOfQuestions = 0;
    if(usedFiftyFifty && score<1)
        fiftyFiftyButton.setEnabled(false);
    if(usedPass && score<1)
        passButton.setEnabled(false);
    Collections.shuffle(numberedQuestions);
    for(int i=0; i<numberedQuestions.size(); i++) {
        numOfQuestions++;
        String question = numberedQuestions.get(i);
        updateQuestionPanel(question,fullQuestion.get(question));
        if(wrongAnswers>=MAX_ERRORS) {
            break;
        }

        if(usedFiftyFifty && score<1)
            fiftyFiftyButton.setEnabled(false);
        else
            fiftyFiftyButton.setEnabled(true);
        if(usedPass && score<1)
            passButton.setEnabled(false);
        else
            passButton.setEnabled(true);
    }

    GUIUtils.showInfoDialog(shell, "Game Over", "Your Final Score is " + score +" After " + numOfQuestions + "Questions");

}

/**
 * Creates the panel that displays the current score
 */
private void createScorePanel() {
    Composite scorePanel = new Composite(shell, SWT.BORDER);
    scorePanel.setLayoutData(GUIUtils.createFillGridData(1));
    scorePanel.setLayout(new GridLayout(2, false));

    final Label label = new Label(scorePanel, SWT.NONE);
    label.setText("Total score: ");

    // The label which displays the score; initially empty
    scoreLabel = new Label(scorePanel, SWT.NONE);
    scoreLabel.setLayoutData(GUIUtils.createFillGridData(1));
}

/**
 * Creates the panel that displays the questions, as soon as the game
 * starts. See the updateQuestionPanel for creating the question and answer
 * buttons
 */
private void createQuestionPanel() {
    questionPanel = new Composite(shell, SWT.BORDER);
    questionPanel.setLayoutData(new GridData(GridData.FILL, GridData.FILL,
            true, true));
    questionPanel.setLayout(new GridLayout(2, true));

    // Initially, only displays a message
    startupMessageLabel = new Label(questionPanel, SWT.NONE);
    startupMessageLabel.setText("No question to display, yet.");
    startupMessageLabel.setLayoutData(GUIUtils.createFillGridData(2));
}

/**
 * Serves to display the question and answer buttons
 */
private void updateQuestionPanel(String question, List<String> answers) {
    // Save current list of answers.
    currentAnswers = answers;

    // clear the question panel
    Control[] children = questionPanel.getChildren();
    for (Control control : children) {
        control.dispose();
    }

    // create the instruction label
    instructionLabel = new Label(questionPanel, SWT.CENTER | SWT.WRAP);
    instructionLabel.setText(lastAnswer + "Answer the following question:");
    instructionLabel.setLayoutData(GUIUtils.createFillGridData(2));

    // create the question label
    questionLabel = new Label(questionPanel, SWT.CENTER | SWT.WRAP);
    questionLabel.setText(question);
    questionLabel.setFont(boldFont);
    questionLabel.setLayoutData(GUIUtils.createFillGridData(2));

    // create the answer buttons
    for (int i = 0; i < 4; i++) {
        Button answerButton = new Button(questionPanel, SWT.PUSH | SWT.WRAP);
        answerButton.setText(answers.get(i));
        GridData answerLayoutData = GUIUtils.createFillGridData(1);
        answerLayoutData.verticalAlignment = SWT.FILL;
        answerButton.setLayoutData(answerLayoutData);

        answerButtons.add(answerButton);
    }

    // create the "Pass" button to skip a question
    passButton = new Button(questionPanel, SWT.PUSH);
    passButton.setText("Pass");
    GridData data = new GridData(GridData.END, GridData.CENTER, true,
            false);
    data.horizontalSpan = 1;
    passButton.setLayoutData(data);

    // create the "50-50" button to show fewer answer options
    fiftyFiftyButton = new Button(questionPanel, SWT.PUSH);
    fiftyFiftyButton.setText("50-50");
    data = new GridData(GridData.BEGINNING, GridData.CENTER, true,
            false);
    data.horizontalSpan = 1;
    fiftyFiftyButton.setLayoutData(data);

    // two operations to make the new widgets display properly
    questionPanel.pack();
    questionPanel.getParent().layout();
}

/**
 * Opens the main window and executes the event loop of the application
 */
private void runApplication() {
    shell.open();
    Display display = shell.getDisplay();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
    boldFont.dispose();
}
}

这是第一堂课.

主类:

public class TriviaMain {

/**
 * Execute to play trivia!
 */
public static void main(String[] args) {
    TriviaGUI gui = new TriviaGUI();
    gui.open();
}

}

当我尝试调试时,程序甚至没有进入 TriviaGUI 类.它立即在 TriviaMain 中崩溃.

The program doesn't even enter the TriviaGUI class when I try to debug. It crashes in the TriviaMain instantly.

推荐答案

您缺少包含该类的 eclipse 库,请在 Eclipse Tye Search 按钮中查找或使用 http://www.findjar.com 或类似的服务来确定 JAR 的调用方式.

You are missing the eclipse library that contains that class, look it in your Eclipse Tye Search button or use http://www.findjar.com or a similar service to identify how is the JAR called.

这篇关于使用 SWT 解决 NoClassDefFoundError 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆