Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
705 views
in Technique[技术] by (71.8m points)

eclipse - "Workspace is closed" exception when trying to get the workspace through ResourcesPlugin

I get this error when trying to get the workspace through ResourcesPlugin:

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

The code generating this is:

IWorkspace ws = ResourcesPlugin.getWorkspace();

Can you please help with this problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...