2013-03-15 124 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