tags:

views:

80

answers:

1

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

+2  A: 

The IResource class is not on your classpath when you start the application.

If you're not using Eclipse (or some other tool) to manage the dependencies, you're going to have to track down every jar file that the Abstract Syntax Tree classes require and manually include them on your classpath. I'm not sure exactly how many this might be, but Eclipse is made up of many dozens of plugins, and manually working out the build dependencies will be a chore.

Edit: To add IResorce to the classpath, the particular jar file you're looking for will be called something like org.eclipse.core.resources_3.5.0.v20090512.jar, depending on your version of Eclipse. But I don't think it will be the only one you'll need...

Ash
Yes, I solved this problem by adding almost all of the org.eclipse.*.jar files that came bundled with the jdt package to my classpath. I don't have time to track down the dependencies, so this was the quickest fix. Thanks for your help!
Shirley Cohen
Maybe you could accept the answer then, to close off the question? Thanks.
Ash