JDT Tutorial: Creating Eclipse Java Projects Programmatically

Aus SDQ-Wiki

I had to create dummy objects for some unit tests. Here is what I did which I hope will help others.


Create a Java project

First create a simple project of type org.eclipse.core.resources.IProject:

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

Because we need a java project, we have to set the Java nature to the created project:

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

Now we can create our Java project

IJavaProject javaProject = JavaCore.create(project); 

However, it's not enough if we want to add Java source code to the project. We have to set the Java build path:

(1) We first specify the output location of the compiler (the bin folder):

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

(2) Define the class path entries. Class path entries define the roots of package fragments. Note that you might have to include the necessary plugin "org.eclipse.jdt.launching".

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));
}
//add libs to project class path
javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);

(3) We have not yet the source folder created:

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

(4) Now the created source folder should be added to the class entries of the project, otherwise compilation will fail:

IPackageFragmentRoot root = 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(root.getPath());
javaProject.setRawClasspath(newEntries, null);

Now the Java project is ready for use. We can generate java classes as follows:

Generate a Java class

We can first create a Java package:

IPackageFragment pack = javaProject.getPackageFragmentRoot(sourceFolder).createPackageFragment(packageName, false, null);

Then we generate a java class by giving only the source code in String format. Suppose the source code of the class we want to create is stored in String variable "source", then:

(1) Don't forget the package declaration:

StringBuffer buffer = new StringBuffer();
buffer.append("package " + pack.getElementName() + ";\n");
buffer.append("\n");
buffer.append(source);

(2) Create the Java class:

ICompilationUnit cu = pack.createCompilationUnit(className, buffer.toString(), false, null);
Keywords: Eclipse, JDT, JDTJDT, Java, Project, Tutorial