运行Java代码错误:工作区已关闭 [英] Run java code error: Workspace is closed
问题描述
要创建一个自动项目,我创建了一个具有以下依赖项的插件项目:
To create an automatic project I created a plug-in project with the following dependencies:
- org.eclipse.core.resources
- org.eclipse.equinox.registry
- org.eclipse.core.runtime
以及位于src文件夹中的以下java类:
And the following java class located in the src folder:
package tester;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
public class tes {
public static void main(String[] args) {
// TODO Auto-generated method stub
IProgressMonitor progressMonitor = new NullProgressMonitor();
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject("DesiredProjectName");
try {
project.create(progressMonitor);
project.open(progressMonitor);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行插件后,我运行了Java类以在eclipse应用程序中创建一个项目,但出现以下错误:
After running the plugin, I ran the java class to create a project in eclipse application, but gave the following error:
Exception in thread "main" java.lang.IllegalStateException: Workspace is closed.
at org.eclipse.core.resources.ResourcesPlugin.getWorkspace(ResourcesPlugin.java:399)
at tester.tes.main(tes.java:15)
我在做什么错?我为我的英语不好而道歉。
What am I doing wrong? And I apologize for my bad English.
推荐答案
您不能只是在中开始使用Eclipse资源。普通Java程序的main
类。您必须使用 eclipse
命令或通过指定 org.eclipse.equinox.launcher.Main
main启动Eclipse。类。这是因为在使工作空间可用之前,需要进行很多初始化。
You can't just start using Eclipse resources in the main
class of an ordinary Java program. You have to start Eclipse using either the eclipse
command or by specifying the org.eclipse.equinox.launcher.Main
main class. This is because there is a lot of initialization that needs to be done before the workspace can be made available.
要运行无头 Eclipse应用程序,应使用 org.eclipse.core.runtime.applications
扩展点用于定义应用程序和要为该应用程序运行的类。像这样的东西:
To run a 'headless' Eclipse application you should use the org.eclipse.core.runtime.applications
extension point to define an application and the class to run for the application. Something like:
<extension
id="appname"
point="org.eclipse.core.runtime.applications">
<application
cardinality="singleton-global"
thread="main"
visible="true">
<run
class="package.Application">
</run>
</application>
</extension>
您在应用程序
eclipse
命令的参数。
这篇关于运行Java代码错误:工作区已关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!