繁体   English   中英

找不到类定义错误

[英]No Class Def Found Error

我在运行程序时收到java.lang.NoClassDefFoundError异常。 我已经在多台计算机上尝试过,但仍然收到相同的错误。 我已经定义了类,但仍然可以找到错误,有人可以帮助我吗?

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)

据说它来自Postfix,它是具有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

我只能猜测,但是您的Postfix类包含静态字段还是静态初始化块? 如果在那里抛出错误,您将得到臭名昭著的NoClassDefFoundError

除此之外,引用为什么我会在Java中收到NoClassDefFoundError?

当您的代码依赖于一个类文件,并且该类文件在编译时存在但在运行时未找到时,会导致这种情况。 在构建时间和运行时类路径中寻找差异。

但是请注意,它并不总是与类路径错误有关:

请参阅其他答案 ,以获取更多详细信息:

...重点是,NoClassDefFoundError不一定是类路径问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM