Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the JRE of java eclipse project in plugin programmatically

I Am trying to create eclipse projects programmatically for my plugin. 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.

So how do you add the JRE programmatically to an project in an eclipse application?

like image 688
Peter Avatar asked Jul 09 '26 21:07

Peter


2 Answers

entries.add(JavaRuntime.getDefaultJREContainerEntry());
like image 82
shyam Avatar answered Jul 12 '26 14:07

shyam


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);               
like image 24
tetchen9 Avatar answered Jul 12 '26 14:07

tetchen9



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!