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.