Runtime Error Using Eclipse Abstract Syntax Tree

I am trying to use the AST parser in an environment without plugins. The code compiles, but I get the following runtime error:

An exception was thrown in the main thread java.lang.NoClassDefFoundError: org / eclipse / core / resources / IResource on org.eclipse.jdt.core.dom.ASTParser. (ASTParser.java:189) at org.eclipse.jdt.core.dom.ASTParser.newParser (ASTParser.java: 118)

Here is the code I'm running:

import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jdt.core.dom.*; public class TestAST { private void runTest() { String helloStr ="\n"+ "public class HelloWorld {\n"+ "\n"+ " private String name=\"\"\n\n"+ " /**\n"+ " * \n"+ " */\n"+ " public void sayHello() {\n"+ " System.out.println(\"Hello \"+name+\"!\");\n"+ " }\n"+ "\n"+ "}"; ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(helloStr.toCharArray()); parser.setResolveBindings(true); ASTNode tree = parser.createAST(null); tree.toString(); } public static void main(String args[]) { TestAST ast = new TestAST(); ast.runTest(); } } 

Does anyone know why this is happening?

Thanks in advance,

Shirley

+5
source share
3 answers

The IResource class IResource not in your class path when the application starts.

If you are not using Eclipse (or some other tool) for dependency management, you will need to track each jar file that the abstract syntax tree classes need and manually include them in your classpath. I'm not sure how much this can be, but Eclipse consists of dozens of plugins, and manual work on build dependencies will be a daunting task.

Edit: To add IResorce to the classpath, the specific jar file you are looking for will be named something like org.eclipse.core.resources_3.5.0.v20090512.jar , depending on your version of Eclipse. But I do not think that this will be the only one that you will need ...

+3
source

I recently encountered a similar problem, and I slowly stepped through fixing one dependency at a time, and here is a list of the necessary dependencies that I encountered. Hope this saves time for people trying to accomplish the same task:

List (corresponds to the figure below):

  • ContentType (org.eclipse.core.contenttype)
  • Jobs (org.eclipse.core.jobs)
  • Resources (org.eclipse.core.resources)
  • Runtime (org.eclipse.core.runtime)
  • Equinox Common (org.eclipse.equinox.common)
  • Equinox Settings (org.eclipse.equinox.preferences)
  • JDT (org.eclipse.jdt)
  • JDT core (org.eclipse.jdt.core)
  • OSGI (org.eclipse.osgi)
  • OSGI Services (org.eclipse.osgi.services)
  • OSGI Util (org.eclipse.osgi.util)

All of these JARs are likely to be contained in your Eclipse plugin directory, and you can find them and add them to the build path by adding them as external JARs.

enter image description here

+3
source

I had the same problem. I decided to add jars to the necessary dependencies of the plugin.xml file. You can find it in the "Dependencies" tab of the plugin.xml file.

0
source

All Articles