views:

907

answers:

2

I get this error:

java.lang.IllegalStateException: Workspace is closed. at org.eclipse.core.resources.ResourcesPlugin.getWorkspace(ResourcesPlugin.java:339)

The code generating it is this:

IWorkspace ws = ResourcesPlugin.getWorkspace();

Can you please help with this problem?

+1  A: 

Does your Manifest.MF contain the org.eclipse.core.resources in the Require-bundle section ?

And do you launch your test as a plugin ? (not as a 'Java Application')

See also this thread.

As mentioned in the "Resources and the file system" help page,

You can access the workspace from the resources plug-in class (defined in org.eclipse.core.resources).

When the resources plug-in is not running, the workspace exists solely in the file system and is viewed or manipulated by the user via standard file-based tools. Let's look at what a workspace looks like on disk as we explain the resources plug-in API.


From this book:

The workspace directory, regardless of the name defined with the -data invocation option, has two roles:

  • it acts as the parent for the .metadata directory
  • and as the default location for projects

the workspace can contains projects only when:

  • the org.eclipse.core.resources plugin is included in the configuration and
  • and appropriately started from the workbench

this is automatic from an IDE configuration based on the org.eclipse.ui.ide.workbench application.

See also this thread and remember that:

the workspace is a different workspace from the runtime-workspace that's used for testing plugins. When you do Run on an Eclipse PDE environment, it creates a new workspace which is completely empty.

The testing workspace root can be specified through the "-data" launching option.

If you want to access a file, your best bet is to include it in the plugin itself, and then use getClass().getResourceAsStream("/myfile.txt") to get an InputStream that you can read the contents for.

VonC
The Manifest.MF file contains org.eclipse.core.resources in the Require-bundle section. But I don't want to run the application as a plug-in. I'm not trying to create a plugin. Just to make use of the AST parser in eclipse.Can I solve this problem and still run the java program as an application?
Jenny Smith
As a Java application, you cannot use ResourcesPlugin functions. So you need to access resources though more classic File resources.
VonC
Also, I forgot to say that I found the fisrt web page myself and done whatever is suggested there. Ans still it didn't work. If I use the ResourcesPlugin class from org.eclipse.core.resources, in the method getWorkspace() the workspace is null. That's why the exception is thrown. Why is this happening? Where is the workspace set?
Jenny Smith
A: 

Try calling Plugin.getStateLocation() in the plugin, it should cause the workspace to be created for you. Without this, you can't save any preferences either. http://dev.eclipse.org/newslists/news.eclipse.platform/msg45020.html

http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/runtime/Plugin.html#getStateLocation%28%29

Err, sorry -- I just completed necroed this question by accident :)

Chris Dennett