Project不是在Eclipse中创建的

时间:2017-05-03 08:08:37

标签: java eclipse

我正在尝试使用处理程序类运行我的插件项目作为应用程序:

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchWindow window =
            HandlerUtil.getActiveWorkbenchWindowChecked(event);
    MessageDialog.openInformation(
        window.getShell(), "MenuEclipseArticle Plug-in",
        "Hello, Eclipse world");
    CreateProject create=new CreateProject();
    IPath  projectLocation =  new Path("C:/Users/monika.sharma/Desktop/software");
    String projectName = "myProject1234";
    create.createProject(projectLocation, projectName);

    return null;
}

我创建了一个 createProject Java类来使用 src bin 文件夹来抓取项目..

public class CreateProject {

    static boolean createAtExternalLocation = false;
     static IPath projectLocation=null;
     private IProject project;
     private IJavaProject javaProject;

    public void createProject(IPath projectLocation,String projectName)
    {
        IProgressMonitor progressMonitor = new NullProgressMonitor();
    IProject project =ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
    IProjectDescription description =ResourcesPlugin.getWorkspace().newProjectDescription(project .getName());
    description.setLocation(projectLocation);

    try {
        project.create(description, progressMonitor);
    } catch (CoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        project.open(progressMonitor);
    } catch (CoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     try {
        setJavaNature();
    } catch (CoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     // Add JRE
     try {
        addDefaultJRE(progressMonitor);
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     // Create the bin folder
     try {
        createBinFolder();
    } catch (CoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     // Create a source folder
     try {
        IPackageFragmentRoot src = createSourceFolder("src");
    } catch (CoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
    public void setJavaNature() throws CoreException {
         // Get the description of the project
         IProjectDescription description = project.getDescription();
         // Change the description with NATURE_ID of JavaCore
         description.setNatureIds(new String[] {JavaCore.NATURE_ID});
         // Set the description back to the project
         project.setDescription(
             description, null);
    }
    public void addDefaultJRE(IProgressMonitor progressMonitor) throws JavaModelException {
         // Create an empty class path entry array for the project
         javaProject.setRawClasspath(
             new IClasspathEntry[0], progressMonitor);
         // Get it - the old entries
         IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
         // Increase 1 for the size of the new entry array.
         IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
         // Copy the old entries to new entry array.
         System.arraycopy(
             oldEntries, 0, newEntries, 0, oldEntries.length);
         // Set the new element to the default JRE of the system.
         newEntries[oldEntries.length] = JavaRuntime.getDefaultJREContainerEntry();
         // Set back entry paths to the project
         javaProject.setRawClasspath(
             newEntries, progressMonitor);
    }
    public IFolder createBinFolder() throws CoreException {
         // Get the folder with the name bin
         IFolder binFolder = project.getFolder("bin");
         // Create this folder. if the first parameter is false, it does not
         // forced to override the folder
         binFolder.create(
             false, true, null);
         // Get the path of the bin folder
         IPath outputLocation = binFolder.getFullPath();
         // Set that path as the output location of the project
         javaProject.setOutputLocation(
             outputLocation, null);
         return binFolder;
    }
    public IPackageFragmentRoot createSourceFolder(String srcName) throws CoreException {
         // Get the folder with srcName name. It may be not exist
         IFolder folder = project.getFolder(srcName);
         // Create a real folder. In the case you want to force override the
         // folder,
         // set the first parameter as true
         folder.create(
             false, true, null);
         // Get the source folder as root package
         IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(folder);
         // Do the samething on the addDefaultJRE method
         IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
         IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
         System.arraycopy(
             oldEntries, 0, newEntries, 0, oldEntries.length);
         // Add a new source folder as a new entry for class paths
         newEntries[oldEntries.length] = JavaCore.newSourceEntry(root.getPath());
         // Set back entry paths to the project
         javaProject.setRawClasspath(
             newEntries, null);
         return root;
    }

当我使用“Run as Eclipse Application”运行MANIFEST.MF文件时,只打开Eclipse但是没有创建项目。

1 个答案:

答案 0 :(得分:1)

'作为Eclipse Application运行'只是启动一个包含你的插件的新Eclipse。新的Eclipse使用单独的工作区。查看'Run>的'Eclipse Applications'部分。 “运行配置”对话框以获取详细信息。

这不会调用您的处理程序代码。处理程序仅在从菜单项或工具栏按钮调用时运行。必须使用org.eclipse.ui.handlers扩展点声明处理程序。有关处理程序的更多详细信息,请阅读this

之类的内容