Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/raedan/public_html/textpattern/lib/txplib_db.php on line 14
State of Flow: Creating Java Projects Programmatically in Eclipse::journal

Creating Java Projects Programmatically in Eclipse

Channing Walton - Monday April 9, 2007

I had to create a Java Project to unit test my EclipseWiki. I found it fiddly and difficult to find information about how to go about it. Here is what I did which I hope will help others.

Actually, whilst I seem to have a working solution that suits my needs, it seems rather complicated. I hope some suggestions will be forthcoming to help me improve it.

So here it is:

First create a progress monitor, mine is the NullProgressMonitor but you can use another if you need to:

IProgressMonitor progressMonitor = new NullProgressMonitor();

Next, create a simple project:

IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject("MyProject");
project.create(progressMonitor);
project.open(progressMonitor);

But, we want a Java project so we need to add the Java nature to the project:

IProjectDescription description = project.getDescription();
String[] natures = description.getNatureIds();
String[] newNatures = new String[natures.length + 1];
System.arraycopy(natures, 0, newNatures, 0, natures.length);
newNatures[natures.length] = JavaCore.NATURE_ID;
description.setNatureIds(newNatures);
project.setDescription(description, progressMonitor);

Now we can finally create a JavaProject:

IJavaProject javaProject = JavaCore.create(project);

But thats not enough if code is going to be added to the project. The project will need its JRE set up:

Set<IClasspathEntry> entries = new HashSet<IClasspathEntry>();
entries.addAll(Arrays.asList(javaProject.getRawClasspath()));
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()]), progressMonitor);

The reason I add all the default VMs libraries to the classpath, instead of just the rt.jar, is because the rt.jar file isn’t in all JREs. For example, it doesn’t exist on the mac. So I thought it easier to add all the classpath entries in the default JRE to the project.

So the question for me is, is all that really necessary? Is there a better way?


  1. Gunnar Wagenknecht    Tuesday April 10, 2007

    Instead of adding the raw library entries I would suggest to use the JRE classpath container.

    Set entries = new HashSet();
    entries.addAll(Arrays.asList(javaProject.getRawClasspath()));
    entries.add(JavaRuntime.getDefaultJREContainerEntry());
    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), progressMonitor);

    Also note that your usage of the progress monitor is only acceptable because it’s the NullProgressMonitor. A better usage would be as follows.

    IProgressMonitor myMonitor=...;

    monitor.beginTask(“Creating Project…”, 4)
    try {

    ... project.create(new SubProgressMonitor(myMonitor, 1)); project.open(new SubProgressMonitor(myMonitor, 1)); ... project.setDescription(description, new SubProgressMonitor(myMonitor, 1)); ... javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), new SubProgressMonitor(myMonitor, 1));

    } finally {

    monitor.done();
    }

    However, the Eclipse IResource API makes it even easier for you. If you don’t want progress reporting or cancellation you can simply pass null as monitor. Unfortunately, the JDT API seems to not allow null.

    PS: Seems like you blog strips all the nice HTML tags like ‘code’ and ‘pre’.

  2. Channing Walton    Tuesday April 10, 2007

    Hi Gunnar, thanks very much for the suggestions.

    Channing

    ps. I’ll see what I can do about the formatting too

  3. Alan Bram    Saturday March 8, 2008

    Thanks for publishing this. I spent a while searching through the Eclipse help looking for something more straightforward, and was just arriving at the conclusion that this was what I was going to have to do. So, it’s nice to have that confirmed.

    By the way, the dates in the blog don’t show the year, so I was curious.

    - arb

  4. Channing Walton    Saturday March 8, 2008

    Hi Alan,
    I’m glad the blog helped you out. I’ve fixed the dates in the blog too.

    Channing

  5. rcat    Tuesday July 15, 2008

    thanks, helped me a lot

  6. Sandeep Gururaj    Saturday July 19, 2008

    Hi, Thanks for putting this up. I have been able to create a Java project programmatically. Hope to get some more help.

    I have the following code after creating the Java project and copying the source packages and files to this project.

    IPackageFragmentRoot[] iPackageFragmentRoots = javaProject.getPackageFragmentRoots();

    and for each element in this iPackageFragmentRoots, I have the following code.

    IJavaElement[] iJavaElements = iPackageFragmentRootElement.getChildren();

    This code does not return me all the Java Elements and packages underneath the “src” folder of the javaProject. I verified that the .classpath file that gets created is proper and no problems with that. However, instead of creating javaProject programmatically, if I create using the Eclipse wizards and pass that as input to the above code, there is no problem.
    Wonder if you have encountered any such problem.

  7. Channing Walton    Sunday July 20, 2008

    Hi Sandeep,
    I don’t know what could be going wrong there, sounds like it could be a bug?

    Channing

  8. Maneesh Rawat    Tuesday December 23, 2008

    Hi

    I need to know how do i create a “Src” folder programatically to this newly created Java Project.

    Thanks and Regards
    Maneesh Rawat

  9. Channing Walton    Tuesday December 23, 2008

    hi,
    not sure but I think you can make use of org.eclipse.jdt.core.JavaCore to do it.

    Channing

  10. sneha    Wednesday January 7, 2009

    hi i hope somebody can help me out with this…i m trying to add programmatically some user libraries…how do i do it..it is smething related to javacore.newlibraryentry…

  11. Channing Walton    Wednesday January 14, 2009

    sneha, I am not sure how to do that but I suspect you are on the right track.

    Channing