Eclipse工作区引用eclipse插件外部

时间:2022-05-21 20:32:52

I'm running the following code:

我正在运行以下代码:

import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;

public class WorkspaceTest {
    public static void main(String[] args) {
        IWorkspace workspace = ResourcesPlugin.getWorkspace();
    }
}

and I get the following error:

我收到以下错误:

Exception in thread "main" java.lang.IllegalStateException: Workspace is closed.

How can I get a reference to the workspace for a non-eclipse plugin?

如何获取非eclipse插件的工作空间引用?

(To be able to run the code here:)

(为了能够在这里运行代码:)

http://www.ssw.uni-linz.ac.at/Teaching/Lectures/KompTech/JDT.pdf

1 个解决方案

#1


First, it can simply means you are not running under Eclipse but instead under just a standard Java application.

首先,它可以简单地表示您不是在Eclipse下运行,而是仅仅在标准Java应用程序下运行。

For an eclipse plugin, you need did to not call it too soon (like before creating workspace). Meaning for a non-eclipse plugin, you may have to somehow create a workspace, since you would not be able to reference the ones already present in eclipse.

对于eclipse插件,你需要做的就是不要太快调用它(比如在创建工作区之前)。对于非eclipse插件的意义,您可能必须以某种方式创建工作空间,因为您将无法引用已经存在于eclipse中的工作空间。

Plus, you have to make sure you do not have org.eclipse.core.resources in the build path but rather as a dependent plugin in the plugin manifest.mf file. (see this thread)
So the ResourcePlugin was not being instantiated by eclipse (although you could still have made calls to the ResourcePlugin class with the code).

另外,您必须确保在构建路径中没有org.eclipse.core.resources,而是在插件manifest.mf文件中作为依赖插件。 (请参阅此主题)因此,eclipse没有实例化ResourcePlugin(尽管您仍然可以使用代码调用ResourcePlugin类)。

See also this answer for other ideas.

另见其他想法的答案。

#1


First, it can simply means you are not running under Eclipse but instead under just a standard Java application.

首先,它可以简单地表示您不是在Eclipse下运行,而是仅仅在标准Java应用程序下运行。

For an eclipse plugin, you need did to not call it too soon (like before creating workspace). Meaning for a non-eclipse plugin, you may have to somehow create a workspace, since you would not be able to reference the ones already present in eclipse.

对于eclipse插件,你需要做的就是不要太快调用它(比如在创建工作区之前)。对于非eclipse插件的意义,您可能必须以某种方式创建工作空间,因为您将无法引用已经存在于eclipse中的工作空间。

Plus, you have to make sure you do not have org.eclipse.core.resources in the build path but rather as a dependent plugin in the plugin manifest.mf file. (see this thread)
So the ResourcePlugin was not being instantiated by eclipse (although you could still have made calls to the ResourcePlugin class with the code).

另外,您必须确保在构建路径中没有org.eclipse.core.resources,而是在插件manifest.mf文件中作为依赖插件。 (请参阅此主题)因此,eclipse没有实例化ResourcePlugin(尽管您仍然可以使用代码调用ResourcePlugin类)。

See also this answer for other ideas.

另见其他想法的答案。