简体   繁体   English

以编程方式在插件中设置java eclipse项目的JRE

[英]Setting the JRE of java eclipse project in plugin programmatically

I Am trying to create eclipse projects programmatically for my plugin. 我正在尝试以编程方式为我的插件创建eclipse项目。 I used this code to create the projects: 我用这段代码来创建项目:

IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
    IProject project = workspaceRoot.getProject(projectName);
    project.create(null);
    project.open(null);

    IProjectDescription description = project.getDescription();
    description.setNatureIds(new String[] { JavaCore.NATURE_ID });
    project.setDescription(description, null);

    IJavaProject javaProject = JavaCore.create(project); 

    IFolder binFolder = project.getFolder("bin");
    binFolder.create(false, true, null);
    javaProject.setOutputLocation(binFolder.getFullPath(), null);

    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();

    IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
    LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
    for (LibraryLocation element : locations) {
        entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
    }

    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);

    IFolder sourceFolder = project.getFolder("src");
    sourceFolder.create(false, true, null);

    IPackageFragmentRoot packageRoot = javaProject.getPackageFragmentRoot(sourceFolder);
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldEntries.length] = JavaCore.newSourceEntry(packageRoot.getPath());
    javaProject.setRawClasspath(newEntries, null);

But as it runs in an eclipse application the JRE system library is not set. 但是当它在eclipse应用程序中运行时,未设置JRE系统库。

So how do you add the JRE programmatically to an project in an eclipse application? 那么如何以编程方式将JRE添加到eclipse应用程序中的项目中呢?

entries.add(JavaRuntime.getDefaultJREContainerEntry());

Works for me. 适合我。

IPath containerPath = new Path(JavaRuntime.JRE_CONTAINER);              
IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
IPath vmPath = containerPath
    .append(vmInstall.getVMInstallType().getId())
    .append(vmInstall.getName());
IClasspathEntry jreEntry = JavaCore.newContainerEntry(vmPath);               

PreferenceConstants.getDefaultJRELibrary(); PreferenceConstants.getDefaultJRELibrary();

The default JRE of an Eclipse is stored in the preferences, so the call above should provide you with the Java Runtime Environment Library. Eclipse的默认JRE存储在首选项中,因此上面的调用应该为您提供Java运行时环境库。

From a more extensive source with great info on creating a Project programmatically: http://www.pushing-pixels.org/2008/11/18/extending-eclipse-creating-a-java-project-without-displaying-a-wizard.html 从更广泛的来源获得有关以编程方式创建项目的重要信息: http//www.pushing-pixels.org/2008/11/18/extending-eclipse-creating-a-java-project-without-displaying-a- wizard.html

            IClasspathEntry[] jreClasspaths=org.eclipse.jdt.ui.PreferenceConstants.getDefaultJRELibrary();

        IClasspathEntry[] oldClasspathEntries=null;
        try{
            oldClasspathEntries=javaProject.getRawClasspath();
        }catch(JavaModelException e){
            e.printStackTrace();
        }

        Set<IClasspathEntry> newClasspathEntries=new HashSet<IClasspathEntry>();
        newClasspathEntries.addAll(Arrays.asList(jreClasspaths));
        newClasspathEntries.addAll(Arrays.asList(oldClasspathEntries));
        try{
            javaProject.setRawClasspath(newClasspathEntries.toArray(new IClasspathEntry[newClasspathEntries.size()]), monitor);
        }catch(JavaModelException e){
            e.printStackTrace();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM