Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Runtime error using the Eclipse Abstract Syntax Tree

I'm trying to use AST parser in a non-plugin environment. The code compiles, but I get the following runtime error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/core/resources/IResource at 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

like image 954
Shirley Cohen Avatar asked Dec 14 '09 22:12

Shirley Cohen


1 Answers

I recently ran into a similar issue and I slowly stepped through fixing one dependency at a time and here is the list of required dependencies that I came up with. I hope this saves some time for people who try to do this same task:

List (which matches picture 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 Preferences (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 these JARs will likely already be contained in your Eclipse plugins directory and you can find and add them to the build path by adding them as external JARs.

enter image description here

like image 157
jbranchaud Avatar answered Sep 21 '22 13:09

jbranchaud