Exception while generating AST using Eclipse JDT SDK in a non-eclipse environment

不想你离开。 提交于 2019-12-12 03:08:27

问题


I'm trying to use the jdt's AST generation feature in a non-eclipse environment(as a plugin for another basic java ide). My program creates the AST correctly when I run it inside eclipse, but when I test the plugin it from the ide I get this exception:

Exception in thread "AWT-EventQueue-0" java.lang.NoSuchFieldError: ignoreMethodBodies 
at org.eclipse.jdt.core.dom.CompilationUnitResolver.parse(CompilationUnitResolver.java:491)
at org.eclipse.jdt.core.dom.ASTParser.internalCreateAST(ASTParser.java:1200) 
at org.eclipse.jdt.core.dom.ASTParser.createAST(ASTParser.java :807) 
at mytreetest.TreeMaker.buildTree(Unknown Source)
...
(further stack trace)
...

The code I've used is quite basic:

Code:

ASTParser parser = ASTParser.newParser(AST.JLS4);
String src = readFile(filePath);
parser.setSource(src.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
Map<String, String> options = JavaCore.getOptions();
JavaCore.setComplianceOptions(JavaCore.VERSION_1_7, options);
parser.setCompilerOptions(options);
CompilationUnit cu = (CompilationUnit) parser.createAST(null);

I went through the source code of org.eclipse.jdt.core.dom.CompilationUnitResolver and org.eclipse.jdt.internal.compiler.impl.CompilerOptions. CompilerOptions does have a public boolean field called ignoreMethodBodies.

What can be causing this error?

Please help me out.

Here are the eclipse jar files I've included :

org.eclipse.core.contenttype_3.4.100.v20110423-0524.jar
org.eclipse.core.jobs_3.5.100.v20110404.jar
org.eclipse.core.resources_3.7100.v20110510-0712.jar
org.eclipse.core.runtime_3.7.O.v20110110.jar
org.eclipse.equinoxcommon_3.6.O.v20110523.jar
org.eclipse.equinox.preferences_3.4.1.R37x_v20110725.jar
org.eclipse.jdt.compiler.apt_1.O.400.v0110816-0800.jar
org.eclipse.jdt.compiler.tooLl.O.100.v_B76_R37x.jar
org.edipse.jdt.core_3.7.1.v_B76_R37x.jar
org.eclipse.jface3.7.0J20110522-1430.jar
org.eclipse.osgi_3.7.1.R37x_v20110808-1106.jar
org.eclipse.osgi.utiL3.2.200.v20110110.jar
org.eclipse.text3.5101 .r371y20110810-O800jar

UPDATE:

I've found the fix for this. The java-ide from which I was running this code was using ecj. The problem was that ecj.jar was clashing with the jdt-core modules I'd included with my plugin. Replacing the ide's ecj with jdt-core(org.eclipse.jdt.core_XX.XX.jar and the rest of the jars) and updating its classpath solved the problem.


回答1:


I used JDT is a Dynamic web project, and got the same error.

Thanks to Manindra Moharana's answer, I fount out the cause of the error.

At first, I added "Apache Tomcat 6.0" server library to the classpath. Unfortunately, Tomcat as well use ecj, which conflict with the CompilerOptions class in jdt.core.

So the solution is remove the Tomcat server library from the classpath.

And now everything seems OK.



来源:https://stackoverflow.com/questions/9995209/exception-while-generating-ast-using-eclipse-jdt-sdk-in-a-non-eclipse-environmen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!