eclipse 尝试通过 ResourcesPlugin 获取工作区时出现“工作区已关闭”异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/962364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Workspace is closed" exception when trying to get the workspace through ResourcesPlugin
提问by Jenny Smith
I get this error when trying to get the workspace through ResourcesPlugin:
尝试通过 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?
你能帮忙解决这个问题吗?
回答by VonC
Does your Manifest.MF
contain the org.eclipse.core.resources
in the Require-bundle
section ?
您的部分中是否Manifest.MF
包含?org.eclipse.core.resources
Require-bundle
And do you launch your test as a plugin ? (not as a 'Java Application')
您是否将测试作为插件启动?(不是“Java 应用程序”)
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.
您可以从资源插件类(在 中定义
org.eclipse.core.resources
)访问工作区。当资源插件未运行时,工作区仅存在于文件系统中,并且由用户通过标准的基于文件的工具查看或操作。在解释资源插件 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.
工作区目录,无论用
-data
调用选项定义的名称如何,都有两个角色:
- 它充当
.metadata
目录的父级- 并作为项目的默认位置
只有在以下情况下,工作区才能包含项目:
- 该
org.eclipse.core.resources
插件包含在配置中,并且- 并从工作台适当地开始
这是从基于
org.eclipse.ui.ide.workbench
应用程序的 IDE 配置自动完成的。
See also this threadand 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.
工作区与用于测试插件的运行时工作区是不同的工作区。当您在 Eclipse PDE 环境中运行时,它会创建一个完全空的新工作区。
The testing workspace root can be specified through the "-data"launching option.
可以通过“-data”启动选项指定测试工作区根。
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 anInputStream
that you can read the contents for.
如果您想访问一个文件,最好的办法是将它包含在插件本身中,然后使用
getClass().getResourceAsStream("/myfile.txt")
获取一个InputStream
您可以读取其内容的文件。
回答by Chris Dennett
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
尝试调用Plugin.getStateLocation()
插件,它应该会为您创建工作区。没有这个,您也无法保存任何首选项。http://dev.eclipse.org/newslists/news.eclipse.platform/msg45020.html
Err, sorry -- I just completed necroed this question by accident :)
呃,对不起 - 我刚刚完成了这个问题 :)