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

이 스택 트레이스를 적어주세요. – Shurmajee

+0

는 어떻게 스레드에서 "기본"java.lang.NoClassDefFoundError가이 프로그램 –

+0

@MayankSharma 예외를 실행 : ExecuteQuiz 인한 기준 : java.lang.ClassNotFoundException가 : ExecuteQuiz \t을 URLClassLoader.java (이는 java.net.URLClassLoader $ 1.run에서 : 202) java.security.AccessController.doPrivileged (원시 메소드)에 java.net.URLClassLoader.findClass \t (URLClassLoader.java:190) java.lang.ClassLoader.loadClass에서 \t (ClassLoader.java:306)에서 \t \t at sun.misc.Launcher $ AppClassLoader.loadClass (Launcher.java:301) \t at java.lang.ClassLoader.loadClass (ClassLoader.java:247) – ShanaBoo

답변

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

+0

그러나 읽는 파일에는 숫자가 아닌 문자가 있습니다. 왜 내가 스위치에 글자가 있는지. 미리 만들어진 텍스트 파일 용입니다. – ShanaBoo

+0

번역 텍스트 만들기 ... 스위치 케이스는 int 값과 함께 작동합니다 .. ENUM과 함께 확인해보십시오 ... 그걸로 작동한다고 생각하지만 확실하지 않습니다 ... –

0

ExecuteQuiz 클래스가 클래스 경로에없는 것 같습니다. classpath에 설정하십시오. 당신이 창에있는 경우

당신이 컴파일러 오류가 아닙니다

set CLASSPATH=%CLASSPATH%.; 
0

다음과 같이 설정할 수 있습니다. 자바 응용 프로그램을 실행하는

일반적인 방법은 경우에 java -classpath <path> FullyQualifiedClassName

FullyQualifiedClassName이

ExecuteQuiz 일단 수업이 클래스와 일치하는 디렉토리 구조를 형성한다 컴파일입니다. 클래스 경로의 일부로 해당 트리의 루트를 제공하십시오. 항아리와 같은 다른 종속성도 추가해야합니다.

더 많은 참조 on how classes are found

관련 문제