尝试通过ResourcesPlugin获取工作区时,“Workspace已关闭”异常

时间:2023-01-24 15:06:24

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?

你能帮忙解决这个问题吗?

2 个解决方案

#1


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

您的Manifest.MF是否包含Require-bundle部分中的org.eclipse.core.resources?

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).

您可以从资源插件类(在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.

当资源插件未运行时,工作空间仅存在于文件系统中,并由用户通过基于标准文件的工具查看或操作。在我们解释资源插件API时,让我们看一下工作区在磁盘上的样子。


From this book:

从这本书:

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

无论使用-data调用选项定义的名称,工作空间目录都有两个角色:

  • it acts as the parent for the .metadata directory
  • 它充当.metadata目录的父级

  • 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
  • org.eclipse.core.resources插件包含在配置中

  • and appropriately started from the workbench
  • 并从工作台适当地开始

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

这是基于org.eclipse.ui.ide.workbench应用程序的IDE配置自动完成的。

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.

工作空间是与用于测试插件的运行时工作空间不同的工作空间。在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 an InputStream that you can read the contents for.

如果你想访问一个文件,最好把它包含在插件本身,然后使用getClass()。getResourceAsStream(“/ myfile.txt”)来获得一个可以读取内容的InputStream。

#2


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

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 :)

呃,对不起 - 我刚刚意外地完成了这个问题:

#1


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

您的Manifest.MF是否包含Require-bundle部分中的org.eclipse.core.resources?

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).

您可以从资源插件类(在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.

当资源插件未运行时,工作空间仅存在于文件系统中,并由用户通过基于标准文件的工具查看或操作。在我们解释资源插件API时,让我们看一下工作区在磁盘上的样子。


From this book:

从这本书:

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

无论使用-data调用选项定义的名称,工作空间目录都有两个角色:

  • it acts as the parent for the .metadata directory
  • 它充当.metadata目录的父级

  • 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
  • org.eclipse.core.resources插件包含在配置中

  • and appropriately started from the workbench
  • 并从工作台适当地开始

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

这是基于org.eclipse.ui.ide.workbench应用程序的IDE配置自动完成的。

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.

工作空间是与用于测试插件的运行时工作空间不同的工作空间。在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 an InputStream that you can read the contents for.

如果你想访问一个文件,最好把它包含在插件本身,然后使用getClass()。getResourceAsStream(“/ myfile.txt”)来获得一个可以读取内容的InputStream。

#2


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

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 :)

呃,对不起 - 我刚刚意外地完成了这个问题: