“工作区已关闭”尝试通过ResourcesPlugin获取工作空间时出现异常 [英] "Workspace is closed" exception when trying to get the workspace through ResourcesPlugin
问题描述
在尝试通过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)
生成这个的代码是:
IWorkspace ws = ResourcesPlugin.getWorkspace();
你能帮忙解决这个问题吗?
Can you please help with this problem?
推荐答案
您的 Manifest.MF
是否包含 org.eclipse.core.resources
在 Require-bundle
部分?
你是否作为插件启动测试? (不是Java应用程序)
And do you launch your test as a plugin ? (not as a 'Java Application')
另请参见这个线程。
正如资源和文件系统帮助页面
您可以从资源插件类(在
org.eclipse.core.resources
中定义)访问工作区。
You can access the workspace from the resources plug-in class (defined in
org.eclipse.core.resources
).
当资源插件未运行时,工作空间仅存在于文件系统中,并由用户通过标准的基于文件的工具进行查看或操作。我们在介绍资源插件API时,看看磁盘上的工作空间。
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.
从本书:
工作区目录,不管使用
-data
调用选项定义的名称有两个角色:
The workspace directory, regardless of the name defined with the
-data
invocation option, has two roles:
- 它作为
.metadata
目录 - 的父级,作为项目的默认位置
工作区只能包含以下项目:
the workspace can contains projects only when:
-
org.eclipse.core.resources
插件包含在配置中, - ,并从工作台中正确启动
- the
org.eclipse.core.resources
plugin is included in the configuration and - and appropriately started from the workbench
这是从基于 org.eclipse.ui.ide.workbench
应用程序的IDE配置自动执行的。
this is automatic from an IDE configuration based on the org.eclipse.ui.ide.workbench
application.
另请参见此主题并记住:
工作空间是用于测试插件的运行时工作区的不同工作区。当您在Eclipse PDE环境中运行时,它将创建一个完全为空的新工作空间。
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.
测试工作区根目录可以通过-data启动选项进行指定。
The testing workspace root can be specified through the "-data" launching option.
如果要访问文件,最好的方法是将其包含在插件本身中,然后使用
getClass() .getResourceAsStream(/ myfile.txt)
以获取您可以阅读内容的InputStream
。
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.
这篇关于“工作区已关闭”尝试通过ResourcesPlugin获取工作空间时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!