0

I'm trying to use ASTParser in eclipse but facing an NoClassDefFoundError. I've already followed some guides and imported related 9 jars. Following are the details:

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));
    
    
}

}

included jars

the error report

command line

How should I get the code running well?

Zhang Tab
  • 11
  • 3
  • This works for me without an error. `ASTParser` should be in the `org.eclipse.jdt.core` JAR. Make sure it's not corrupted by navigating to this class. Please show the command line (in the run configuration there is a _Show Command Line_ button for that). – howlger Feb 16 '21 at 16:45
  • Thanks so much for your reply, I also think there shouldn't be an error, it's confusing. I attch comand line picture in the question case it's too long for comment. – Zhang Tab Feb 17 '21 at 00:58
  • In _Project > Properties: Java Build Path_, does moving the JARs from the _Modulepath_ to the _Classpath_ help? – howlger Feb 17 '21 at 06:51
  • THANKS YOU SO MUCH for your help, Howlger. Moving them to class path does solve the problem. It works well now. – Zhang Tab Feb 17 '21 at 11:00

1 Answers1

0

In Project > Properties: Java Build Path, tab Libraries move all the JARs from the Modulepath to the Classpath.

howlger
  • 31,050
  • 11
  • 59
  • 99