views:

566

answers:

1

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

A: 

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.

VonC
>Plus, you have to make sure you do not have...Are you saying it can't be done outside a plugin? Or are you saying I need to find another way to instantiate the ResourcePlugin?
hawkeye
From what I have seen, it seems you have to access Workspace from within a plugin running within eclipse (and not as an independent Java application)
VonC
How do I run it as a plugin instead of a Java application?
yeeen
You can run it as an "Eclipse Application"
VonC
Thanks. But how can I run this project here? http://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation%5FAST/index.html (The link to download this project is under "Resources"[1]). After launching as an "Eclipse Application", I only see Eclipse opening up, I see nothing else.
yeeen
There is a method in the class "public class ASTArticleActionDelegate implements IObjectActionDelegate"... It seems like run(...) is the starting point, can u tell what am I suppose to do to expect sth?public void run(IAction action) { if (selection instanceof IStructuredSelection) { ICompilationUnit lwUnit = (ICompilationUnit) ((IStructuredSelection) selection) .getFirstElement(); createActionExecutable(action.getId()).run(lwUnit); } }
yeeen
I have oso posted my qn here: http://stackoverflow.com/questions/1383139/running-an-eclipse-pluginUrgent, thx...
yeeen
Sorry yeeen, I just got back to my station today. I left you a detailed answer at your question http://stackoverflow.com/questions/1383139/running-an-eclipse-plugin
VonC