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?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With