【发布时间】:2021-02-16 15:48:10
【问题描述】:
我正在尝试在 Eclipse 中使用 ASTParser,但遇到了 NoClassDefFoundError。 我已经遵循了一些指南并导入了相关的 9 个罐子。 详情如下:
package test_JDT;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
// import org.eclipse.equinox.common.*;
// import org.eclipse.core.jobs;
class tester {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("hello ast parser");
String javaFilePath = "C:\\Users\\tabzhang\\eclipse-workspace\\test_JDT\\src\\test_JDT/classdemo.java";
byte[] input = null;
try {
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(javaFilePath));
input = new byte[bufferedInputStream.available()];
bufferedInputStream.read(input);
bufferedInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String str = new String(input);
System.out.println(str);
ASTParser astParser = ASTParser.newParser(AST.JLS3);
astParser.setSource(new String(input).toCharArray());
astParser.setKind(ASTParser.K_COMPILATION_UNIT);
CompilationUnit result = (CompilationUnit) (astParser.createAST(null));
}
}
我应该如何让代码运行良好?
【问题讨论】:
-
这对我有用,没有错误。
ASTParser应该在org.eclipse.jdt.coreJAR 中。通过导航到此类,确保它没有损坏。请显示命令行(在运行配置中有一个显示命令行按钮)。 -
非常感谢您的回复,我也认为应该没有错误,它令人困惑。我在问题案例中附加了命令行图片,评论太长了。
-
在 Project > Properties: Java Build Path 中,将 JAR 从 Modulepath 移动到 Classpath 有帮助吗?
-
非常感谢您的帮助,豪格。将它们移动到类路径确实可以解决问题。现在效果很好。
标签: java eclipse jar eclipse-jdt fastparse