2013-03-15 120 views
0

運行此代碼時出現以下錯誤:java.lang.NoClassDefFoundError如何解決這個問題?

下面是完整的Stackflow,任何輸入將讚賞;

Exception in thread "main" java.lang.NoClassDefFoundError: ExecuteQuiz 
Caused by: java.lang.ClassNotFoundException: ExecuteQuiz 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247) 

任何想法爲什麼?我有7個其他類參與該項目,但從未見過這個錯誤。

import java.io.File; 
    import java.io.IOException; 
    import java.util.ArrayList; 
    import java.util.Collections; 
    import java.util.Scanner; 

    public class ExecuteQuiz { 

    static Scanner input = new Scanner(System.in); 

    public static void main(String[] args) throws IOException { 
     // ask the user for the filename 
     Scanner scan = new Scanner(System.in); 

     System.out.print("Which quiz are you taking? "); 
     String theFile = scan.nextLine(); // file may contain more than one word 
     File fileIn = new File(theFile); 

     // ask the user for another filename if the given file doesn't exist 
     // exists() method in File class - checks whether file 
     // exists and is readable 
     while (!fileIn.exists()) { 
      System.out.print("Invalid file name! Try again: "); 

      theFile = scan.nextLine(); 

      fileIn = new File(theFile); 
     } 

     // have a valid file name, create a Scanner object 
     Scanner fileScan = new Scanner(fileIn); 

     // An arraylist of ALL problems. 
     ArrayList<Problem> problems = new ArrayList<Problem>(); 

     // process the file 
     while (fileScan.hasNextLine()) { 
      String type = scan.nextLine(); // Get the line in a string 
      String question = scan.nextLine(); 

      switch (type) { 
      case "W": 
       String WAnswer = scan.nextLine(); 
       WProblem w = new WProblem(question, WAnswer); 
       problems.add(w); 
       break; 
      case "T": 
       String TString = scan.nextLine(); // Gets the string 
       boolean TAnswer = Boolean.parseBoolean(TString); // Converts to 
                    // boolean 
       TProblem t = new TProblem(question, TAnswer); // Creates the 
                   // object 
       problems.add(t); 
       break; 
      case "N": 
       String Nanswer = scan.nextLine(); 
       NProblem n = new NProblem(question, Nanswer); 
       problems.add(n); 
       break; 
      case "S": 
       ArrayList<String> options = new ArrayList<String>(); 
       // Get the answer and add it to the options 
       String SAnswer = input.nextLine(); 
       options.add(SAnswer); 

       // add the rest of options 
       while (input.nextLine() != null) { 
        String option = input.nextLine(); 
        options.add(option); 
       } 
       // Create new objects 
       SProblem s = new SProblem(question, SAnswer, options); 

       problems.add(s); 
       break; 
      case "M": 
       ArrayList<String> MAnswer = new ArrayList<String>(); 
       ArrayList<String> MOptions = new ArrayList<String>(); 

       // Find all the answers 
       while (input.nextLine() != null) { 
        String answer = input.nextLine(); 
        MAnswer.add(answer); 
        MOptions.add(answer); 
       } 

       // get the rest of the options 
       while (input.nextLine() != null) { 
        MOptions.add(input.nextLine()); 
       } 

       MProblem m = new MProblem(question, MAnswer, MOptions); 
       problems.add(m); 
       break; 
      case "O": 
       // Adding answers into an arraylist 
       ArrayList<String> OrderedAnswer = new ArrayList<String>(); 

       // Add the answers in order 
       while (input.nextLine() != null) { 
        OrderedAnswer.add(input.nextLine()); 
       } 
       OProblem o = new OProblem(question, OrderedAnswer); 
       problems.add(o); 
       break; 
      } 
      // Analyze the type of problem 

     } 
    } 
} 
+1

請給堆棧跟蹤。在java.net.URLClassLoader的$ 1.run(URLClassLoader.java ExecuteQuiz \t: – Shurmajee 2013-03-15 04:57:30

+0

你是如何運行的程序 – 2013-03-15 04:58:16

+0

@MayankSharma異常在線程 「主要」 java.lang.NoClassDefFoundError:ExecuteQuiz 引起:拋出java.lang.ClassNotFoundException: 202) \t在java.security.AccessController.doPrivileged(本機方法) \t在java.net.URLClassLoader.findClass(URLClassLoader.java:190) \t在java.lang.ClassLoader.loadClass(ClassLoader.java:306) \t at sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:301) \t at java.lang.ClassLoader.loadClass(ClassLoader.java:247) – ShanaBoo 2013-03-15 04:59:39

回答

1

你的類具有重大的編譯時間的問題。你甚至編譯過代碼並檢查了嗎?

開關的情況下被賦予錯誤編譯:不兼容的類型

類沒有編譯,你想直接運行it..thus無類定義中發現的錯誤。

試圖改變部分:

case "W": 

case 1: 

編譯並運行

我用下面的代碼:

import java.io.File; 
import java.io.IOException; 

import java.util.ArrayList; 
import java.util.Scanner; 


public class ExecuteQuiz { 

    static Scanner input = new Scanner(System.in); 

    public static void main(String[] args) throws IOException { 
     // ask the user for the filename 
     Scanner scan = new Scanner(System.in); 

     System.out.print("Which quiz are you taking? "); 
     String theFile = scan.nextLine(); // file may contain more than one word 
     File fileIn = new File(theFile); 

     // ask the user for another filename if the given file doesn't exist 
     // exists() method in File class - checks whether file 
     // exists and is readable 
     while (!fileIn.exists()) { 
      System.out.print("Invalid file name! Try again: "); 

      theFile = scan.nextLine(); 

      fileIn = new File(theFile); 
     } 

     // have a valid file name, create a Scanner object 
     Scanner fileScan = new Scanner(fileIn); 

     // An arraylist of ALL problems. 
     ArrayList<Problem> problems = new ArrayList<Problem>(); 

     // process the file 
     while (fileScan.hasNextLine()) { 
      int type = Integer.parseInt(scan.nextLine()); // Get the line in a string 
      String question = scan.nextLine(); 

      switch (type) { 
      case 1: 
       String WAnswer = scan.nextLine(); 
       WProblem w = new WProblem(question, WAnswer); 
       problems.add(w); 
       break; 
      case 2: 
       String TString = scan.nextLine(); // Gets the string 
       boolean TAnswer = Boolean.parseBoolean(TString); // Converts to 
       // boolean 
       TProblem t = new TProblem(question, TAnswer); // Creates the 
       // object 
       problems.add(t); 
       break; 
      case 3: 
       String Nanswer = scan.nextLine(); 
       NProblem n = new NProblem(question, Nanswer); 
       problems.add(n); 
       break; 
      case 4: 
       ArrayList<String> options = new ArrayList<String>(); 
       // Get the answer and add it to the options 
       String SAnswer = input.nextLine(); 
       options.add(SAnswer); 

       // add the rest of options 
       while (input.nextLine() != null) { 
        String option = input.nextLine(); 
        options.add(option); 
       } 
       // Create new objects 
       SProblem s = new SProblem(question, SAnswer, options); 

       problems.add(s); 
       break; 
      case 5: 
       ArrayList<String> MAnswer = new ArrayList<String>(); 
       ArrayList<String> MOptions = new ArrayList<String>(); 

       // Find all the answers 
       while (input.nextLine() != null) { 
        String answer = input.nextLine(); 
        MAnswer.add(answer); 
        MOptions.add(answer); 
       } 

       // get the rest of the options 
       while (input.nextLine() != null) { 
        MOptions.add(input.nextLine()); 
       } 

       MProblem m = new MProblem(question, MAnswer, MOptions); 
       problems.add(m); 
       break; 
      case 6: 
       // Adding answers into an arraylist 
       ArrayList<String> OrderedAnswer = new ArrayList<String>(); 

       // Add the answers in order 
       while (input.nextLine() != null) { 
        OrderedAnswer.add(input.nextLine()); 
       } 
       OProblem o = new OProblem(question, OrderedAnswer); 
       problems.add(o); 
       break; 
      } 
      // Analyze the type of problem 

     } 
    } 
} 
+0

用戶可能位於jdk 7上,該命令行語法有所改進。 – Jayan 2013-03-15 05:04:56

+0

但是它正在閱讀的文件將包含字母,而不是數字。因此,爲什麼我在交換機中有字母。這是用於預製的文本文件。 – ShanaBoo 2013-03-15 05:05:13

+0

使翻譯文本...開關的情況下只適用於int值.. 也試圖檢查它與ENUMs ...我認爲它的作品,但我不知道... – 2013-03-15 05:06:13

0

看起來ExecuteQuiz類不在classpath上。將它設置在classpath中。

如果您使用的是Windows,你可以將其設置爲以下

set CLASSPATH=%CLASSPATH%.; 
0

這不是一個編譯器錯誤。

啓動java應用程序的典型方式是java -classpath <path> FullyQualifiedClassName

FullyQualifiedClassName你的情況是ExecuteQuiz

一旦編譯你的類將形成匹配類的目錄結構。提供該樹的根作爲類路徑的一部分。你還應該添加任何其他依賴像罐子。

更多參考on how classes are found