2017-06-14 79 views
0

當我運行程序時,得到java.lang.NoClassDefFoundError異常。我曾在多臺電腦上嘗試過,但仍然出現相同的錯誤。我有定義的類我仍然可以找到任何人都可以幫助我的錯誤?沒有類Def的發現錯誤

public class PostfixDriver { 
    public static void main(String[] args) { 
System.out.println("Testing postfix expressions with\n" + 
    "a = 2.0, b = 3.0, c = 4.0, d = 5.0\n"); 
    testPostfix("a+b"); 
testPostfix("a-b+c*d"); 
    testPostfix("(a+b)*c-d"); 
    testPostfix("a+b*(c-d)"); 
testPostfix("(a+b)/(c-d)"); 
    testPostfix("a*(b/(c-d))"); 
} // end main 

public static void testPostfix(String infixExpression) { 
String postfixExpression = Postfix.convertToPostfix(infixExpression); 
System.out.println("Infix: " + infixExpression); 
System.out.println("Postfix: " + postfixExpression); 
    System.out.println("Value: " + 
Postfix.evaluatePostfix(postfixExpression)); 
    System.out.println(); 
    } // end testPostfix 
    } // end PostfixDriver 

這是完全錯誤

Exception in thread "main" java.lang.NoClassDefFoundError: Postfix 
at PostfixDriver.testPostfix(PostfixDriver.java:17) 
at PostfixDriver.main(PostfixDriver.java:8) 
Caused by: java.lang.ClassNotFoundException: Postfix 
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 

它說這是從後綴來這是具有converttoPostfix階級和一切是一樣的。如果需要,我可以將它張貼。

Postfix.java

public static String convertToPostfix(String infix) { 
    ArrayStack operator = new ArrayStack(); 
    String item; 
    String postfixe; 
    Object top; 



    for(int i = 0; infix.length() >= 0; i++){ 
    item.charAt(i); 


    if (Postfix.isVariable(item.charAt(i))){ 
    postfixe.concat(item.toString());   
    } 

    else if (item.charAt(i) == '('){ 
    operator.push(item); 
    } 
    else if (item.charAt(i) == ')'){ 
    Postfix.concat(item.toString()); 
    while(!top.equals('(')){ 
     postfixe.concat(item.toString()); 
     top = operator.pop(); 
    } 
    } 
    else { 
    while(!operator.isEmpty()){ 
     top = operator.peek(); 

     if(Postfix.getPrecedence(item.charAt(i)) <= (Character)top){ 
      postfixe.concat(item); 
      operator.pop(); 
     } 
     else { 
      break; 
     } 
     operator.push(item); 

    } 
    } 


    } 

while(!operator.isEmpty()){ 
    top = operator.pop(); 
    Postfix.concat(item); 

    } 

    return postfixe; 







    } // end convertToPostfix 
+1

'Postfix'類在哪裏? –

+0

它與PostfixDriver.java在同一個文件夾中 – thechamp

+1

[爲什麼我在Java中得到一個NoClassDefFoundError?](https://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror -in-JAVA) –

回答

0

我只能猜測,但你Postfix類包含靜態字段或靜態初始化塊?如果有錯誤發生,你會得到臭名昭着的NoClassDefFoundError

在那旁邊,從Why am I getting a NoClassDefFoundError in Java?舉:

這時候還有一類文件,你的代碼依賴於它出現在編譯時間,但在運行時沒有發現造成的。查找構建時間和運行時類路徑的差異。

但是請注意,它並不總是有類路徑錯誤的事:

other answer,瞭解詳情:

...關鍵是,一個NoClassDefFoundError的不一定類路徑問題。