title: Solving NoClassDefFoundError exception with SWT
body: <p>I am trying to create a program using the SWT library, but when I try to run it I get the following exception:</p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>It doesn't even start to run the program, it just crashes instantly. What can be done to solve it? </p>
  
  <p>I have added my code:</p>
  
  <pre><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&lt;Button&gt; answerButtons = new LinkedList&lt;&gt;();
  private Button passButton;
  private Button fiftyFiftyButton;
  
  //other
  private String path="";
  private List&lt;String&gt; currentAnswers;
  private SortedMap&lt;String,List&lt;String&gt;&gt; fullQuestion = new TreeMap&lt;&gt;();
  private Map&lt;String,String&gt; questionPlusAnswer = new HashMap&lt;&gt;();
  private ArrayList&lt;String&gt; numberedQuestions = new ArrayList&lt;&gt;();
  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&lt;String&gt; anwersForThisQuestion = new ArrayList&lt;&gt;();
                      for(int i=1; i&lt;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 &amp;&amp; score&lt;1)
          fiftyFiftyButton.setEnabled(false);
      if(usedPass &amp;&amp; score&lt;1)
          passButton.setEnabled(false);
      Collections.shuffle(numberedQuestions);
      for(int i=0; i&lt;numberedQuestions.size(); i++) {
          numOfQuestions++;
          String question = numberedQuestions.get(i);
          updateQuestionPanel(question,fullQuestion.get(question));
          if(wrongAnswers&gt;=MAX_ERRORS) {
              break;
          }
  
          if(usedFiftyFifty &amp;&amp; score&lt;1)
              fiftyFiftyButton.setEnabled(false);
          else
              fiftyFiftyButton.setEnabled(true);
          if(usedPass &amp;&amp; score&lt;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&lt;String&gt; 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 &lt; 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();
  }
  }
  </code></pre>
  
  <p>This is the first class.</p>
  
  <p>The main class: </p>
  
  <pre><code>public class TriviaMain {
  
  /**
   * Execute to play trivia!
   */
  public static void main(String[] args) {
      TriviaGUI gui = new TriviaGUI();
      gui.open();
  }
  
  }
  </code></pre>
  
  <p>The program doesn't even enter the TriviaGUI class when I try to debug. It crashes in the TriviaMain instantly.</p>