java.lang.IllegalStateException: Workspace is closed.

This post belongs to Eclipse JDT Tutorial Series.

This error message starts the whole JDT ASTParser tutorial series.

Exception in thread “main” java.lang.IllegalStateException: Workspace is closed.
at org.eclipse.core.resources.ResourcesPlugin.getWorkspace(ResourcesPlugin.java:340)
at Main.main(Main.java:20)

In brief, this is caused by simply adding dependent jar files to regular java project.

To use JDT, you need to have the program running as a plug-in (or at least, an OSGi-enabled application) rather than using it as a jar.

Step by step tutorials can be found by searching button on the right sidebar:

1. How to create a plug-in project?
2. How JDT works?
3. A complete example to parse a java project.

2 thoughts on “java.lang.IllegalStateException: Workspace is closed.”

  1. Hi,
    I am getting the same exception in my project:

    Exception in thread “main” java.lang.ExceptionInInitializerError
    at ASTModifier.main(ASTModifier.java:205)
    Caused by: java.lang.IllegalStateException: Workspace is closed.
    at org.eclipse.core.resources.ResourcesPlugin.getWorkspace(ResourcesPlugin.java:340)
    at org.eclipse.core.filebuffers.FileBuffers.(FileBuffers.java:52)
    … 1 more
    ——
    I am using JDT for creating abstract syntax tree of source java file and then I am modifying the ast using ASTRewrite. Till this it works fine.
    But now I want to reflect changes made by ASTRewrite to be reflected in original source code file, (see link: http://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation_AST/index.html)
    Refer “Write it Down” section.

    As per the above tutorial, I am declaring
    ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();

    Above declaration throws the error. Can you pls tell me what is the reason? Do I have to create a plugin for performing this operation? Is there any other way also?

Leave a Comment