Search code examples
javaeclipseworkspace

Eclipse workspace reference outside eclipse plugin


I'm running the following code:

import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;

public class WorkspaceTest {
    public static void main(String[] args) {
        IWorkspace workspace = ResourcesPlugin.getWorkspace();
    }
}

and I get the following error:

Exception in thread "main" java.lang.IllegalStateException: Workspace is closed.

How can I get a reference to the workspace for a non-eclipse plugin?

(To be able to run the code here:)

http://www.ssw.uni-linz.ac.at/Teaching/Lectures/KompTech/JDT.pdf


Solution

  • First, it can simply means you are not running under Eclipse but instead under just a standard Java application.

    For an eclipse plugin, you need did to not call it too soon (like before creating workspace). Meaning for a non-eclipse plugin, you may have to somehow create a workspace, since you would not be able to reference the ones already present in eclipse.

    Plus, you have to make sure you do not have org.eclipse.core.resources in the build path but rather as a dependent plugin in the plugin manifest.mf file. (see this thread)
    So the ResourcePlugin was not being instantiated by eclipse (although you could still have made calls to the ResourcePlugin class with the code).

    See also this answer for other ideas.