简体   繁体   中英

java.lang.NoSuchFieldError: resources - Eclipse AST parser error

I am trying to use Eclipse's AST parser as a standalone to parse Java source files. This is a small snippet of a code to get the compilation unit:

public CompilationUnit getCompilationUnit(ASTParser parser) {
    parser.setResolveBindings(true);
    CompilationUnit cu = (CompilationUnit) parser.createAST(null);
    return cu;
}

If I try to execute this code standalone ie without Eclipse IDE, I am getting below error:

java.lang.NoSuchFieldError: resources
at org.eclipse.jdt.core.dom.ASTConverter.convert(ASTConverter.java:2545) [org.eclipse.jdt.core_3.8.2.v20120814-155456.jar:]
at org.eclipse.jdt.core.dom.ASTConverter.convert(ASTConverter.java:2423) [org.eclipse.jdt.core_3.8.2.v20120814-155456.jar:]
at org.eclipse.jdt.core.dom.ASTConverter.convert(ASTConverter.java:534) [org.eclipse.jdt.core_3.8.2.v20120814-155456.jar:]

I am not understanding from where does it's calling method resources . If I try to run from Eclipse IDE it works without any issues. I have included following JAR files in the classpath:

org.eclipse.core.contenttype_3.4.200.v20120523-2004.jar
org.eclipse.core.jobs_3.5.300.v20120912-155018.jar
org.eclipse.core.resources_3.8.1.v20121114-124432.jar
org.eclipse.core.runtime_3.8.0.v20120912-155025.jar
org.eclipse.equinox.common_3.6.100.v20120522-1841.jar
org.eclipse.equinox.preferences_3.5.1.v20121031-182809.jar
org.eclipse.jdt.core_3.8.3.v20130121-145325.jar
org.eclipse.osgi_3.8.2.v20130124-134944.jar

I have included all of the necessary jar files to run successfully. But when I run this error occurs. Can anyone please guide me?

I can't see who calls the getCompilationUnit method but in order to use ASTParser you must to set unit name and set environment (as I said I am not sure if you have done that), the following code snippet might help you:

// creating ASTParser
ASTParser parser = ASTParser.newParser(AST.JLS4);
// you probably want to set the kind
parser.setKind(ASTParser.K_COMPILATION_UNIT);
// you must set compilation unit name, so I just name it as a path to source file
parser.setUnitName(inputFilePath.toString());
// you must set environment params classpathEntries,  sourcepathEntries, encodings, IncludeRunningVMBootclasspath
parser.setEnvironment(null, null, null, true);
// you probably want to set source
parser.setSource(fileContent);
// I can see that you actually want to do that :)
parser.setResolveBindings(true);
// creating compilation unit
CompilationUnit unit = (CompilationUnit) parser.createAST(null);

I had also faced the similar issue.

Check the compatibility of your java version with your jar version. I am using Java8 so using the below jar.

Tried using the latest jar eclipse-astparser and it works. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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