English 中文(简体)
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 ="
"+
    "public class HelloWorld {
"+
    "
"+
    "   private String name=""

"+
    "   /**
"+
    "    * 
"+
    "    */
"+
    "    public void sayHello() {
"+
    "    System.out.println("Hello "+name+"!");
"+
    "    }
"+
    "
"+
    "}";

    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

问题回答

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

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...

I had the same problem. I solved adding the jars into the required dependencies of the the plugin.xml. You can find it in the tab Dependencies of the plugin.xml file.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签