java构建工作区出现问题,运行Java代码错误:工作区已关闭

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

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();

}

}

}

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.

解决方案

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.

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:

id="appname"

point="org.eclipse.core.runtime.applications">

cardinality="singleton-global"

thread="main"

visible="true">

class="package.Application">

You specify this application id on the -application parameter of the eclipse command.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值