A complete standalone example of ASTParser

This example belongs to Eclipse JDT Tutorial Series.

Normally, Eclipse JDT is used in a plug-in environment. This is the normal situation in which we deal with each project in the workspace. However, if we want to parse a large amount of java file, this approach may not be good, since it is not easy to import many projects to workspace manually. (We can import projects automatically, but that's another story.)

Can we use Eclipse's ASTParser to parse java source code in a standalone application, instead of a plug-in? I asked the same question, before I found the solution. Here is a complete code example which ASTParser works in a standalone application.

import java.util.HashSet;
import java.util.Set;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
public class Test {
	public static void main(String args[]){
		ASTParser parser = ASTParser.newParser(AST.JLS3);
		parser.setSource("public class A { int i = 9;  \n int j; \n ArrayList<Integer> al = new ArrayList<Integer>();j=1000; }".toCharArray());
		//ASTNode node = parser.createAST(null);
		final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
		cu.accept(new ASTVisitor() {
			Set names = new HashSet();
			public boolean visit(VariableDeclarationFragment node) {
				SimpleName name = node.getName();
				System.out.println("Declaration of '"+name+"' at line"+cu.getLineNumber(name.getStartPosition()));
				return false; // do not continue to avoid usage info
			public boolean visit(SimpleName node) {
				if (this.names.contains(node.getIdentifier())) {
				System.out.println("Usage of '" + node + "' at line " +	cu.getLineNumber(node.getStartPosition()));
				return true;

It will print out the following message:

Declaration of 'i' at line1
Declaration of 'j' at line2
Declaration of 'al' at line3

All problems, such as "The type org.eclipse.core.runtime.IProgressMonitor cannot be resolved. It is indirectly referenced from required .class files", etc, are caused by missing libraries.

Here is the jar files required:


The following are what I used for this sample program.

required jar for JDT ASTParser

These jar files will be under plugins directory, once you installed astview plugin. Here is post about how to add astview plugin. For convenience, I also made a rar file which includes all required jar files. You can download it directly here.

This is another article that might be useful for you to understand AST.

Category >> Java Development Tools (JDT)  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";

  1. giri on 2011-7-12

    nice article ! u should also point to the usage
    of the anonymous class – thats a typical
    pattern in these kind of projects

  2. admin on 2011-7-13

    Thanks. That’s a very good idea.

  3. sagar on 2011-8-5
  4. sagar on 2011-8-5

    Also, How can I parse the annotations of the method. For ex, I am writing a contract for a method using cofoja’s @ensures and @requires, how can i parse the annotations of these methods.?

  5. Admin on 2011-8-6

    There is a plugin “ASTView” like firebug which let you see explore ast easily. http://www.eclipse.org/jdt/ui/astview/index.php

  6. Fabricio on 2011-9-22

    I’m facing the following problem in the line 23:

    The type org.eclipse.core.runtime.IProgressMonitor cannot be resolved. It is indirectly referenced from required .class files

    Here is the code:

    final CompilationUnit cu = (CompilationUnit)parser.createAST(null);

    Does anyone have any clue?

    Thanks in advance!

  7. Fabricio on 2011-9-23

    I’ve just found that the following jar reference was the cause:


  8. spyros on 2011-9-24

    hi, i want to get info from every class of a .java file so that i can save in a structure (maybe a list) the methods that every method of every class calls. Can anyone help me? thanx!

  9. Fabricio on 2011-9-27

    Hi spyros,

    Check out the following post. It may help you.



  10. Admin on 2011-9-27

    Have you tried AST Viewer? You can first use it to explore the structure of AST and then may get the idea about how to select elements you want.

  11. abdul on 2011-10-6

    how can i set a .java file as an ICompilationUnit in setSource function? tnx

  12. Ashwin Jayaprakash on 2011-10-18

    Thanks for writing this. I was disappointed to see that there is no official Eclipse package that includes the compiler and ASTparser.

    I have posted this question on the forum – http://www.eclipse.org/forums/index.php/m/740970/#msg_740970

  13. Gleiph on 2012-11-8

    Hi!!! I’m trying to use JDT, but I would like to use it for any project. This is, a project that is not in the Eclipse workspace. How can I load a project and use JDT to rewrite it? Thanks,


  14. Oliver on 2013-7-4

    Thanks for this nice snippet, it really worked on the first go in less than 5 minutes. 🙂

  15. Rohan on 2014-1-3

    Thanks for this post – it’s really the quickest way to get started with a standalone parser using Eclipse JDT.

  16. Moj MN on 2014-3-7

    Thanks for this post.
    it is really good post for method body text extract.
    use this post i able to extract method body variable and define location of invoke other method

  17. Richard Le on 2014-3-19

    Nice post! I really appreciate your time and your dedication. You collected and gave us all jar library files. This save tons of time for us.
    Again, thank you so much.

  18. cuniquek87 . on 2014-4-22

    nice post!how can i get method names and method inputs?

  19. Amol Sawant on 2014-5-8

    Thanks you very much !!!
    Appreciate ur efforts, Mr. Wang.. Grateful

  20. ricardojlrufino on 2014-12-14

    An example using less dependencies and a TreeView.


  21. SELASSI ABDELLAH on 2015-1-24

    Thanks I think the same 😉

  22. bniyaseen on 2015-2-26

    how i can get the name of class the method invoked inside it

    class A{


    i need the result like this

    “method invoked” : getmethod()

    “in class”: A

  23. ryanlr on 2015-2-26

    You can parse .java file instead http://www.programcreek.com/2011/11/use-jdt-astparser-to-parse-java-file/. Then in your defined visitor, visit TypeDeclaration and record the name:

    public boolean visit(VariableDeclarationFragment node){

  24. Hevayo on 2015-7-10

    Thanks this was very helpful.

  25. Adnan Qureshi on 2015-8-21

    hi Can you please share your code . i am working in this project but could not find exact solutions ..

Leave a comment