DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Debug Like a Pro in 2025: 10 New Eclipse Java Debugger Features to Enhance Your Productivity (With Spring Boot Examples)
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Trending

  • PWA on iOS, Part 2: HTTPS, Manifest and Service Worker
  • Closing the Drift Loop: An AI Agent That Drafts Terraform Fixes
  • Applying Microservices Lessons to Multi-Agent AI Systems
  • Genkit Flows as senro Steps: Extremely Powerful AI Pipelines in Go

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Frameworks
  4. Use Eclipse JDT to dynamically create, access, and load projects

Use Eclipse JDT to dynamically create, access, and load projects

By 
Ryan Wang
·
Mar. 21, 13 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
8.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

in this article, we are going to use eclipse jdt to create, access and load projects. i assume that you know how to create a simple eclipse plug-in project which adds a menu item that you can click and trigger some actions. if you don’t know, you can go to this article . the reason why we need a plug-in project is that java model only work inside of a plug-in, a standalone application will not support java model.

this article only focus on jdt java model. the following three topics will be explored:

  • create projects in workspace
  • access projects in workspace
  • dynamically import existing projects into workspace

those are essentially important when you want to process a large number of java projects.

1. create projects

we can use java model to create a new project in the work space.

the following here requires the following dependencies:

import org.eclipse.core.resources.ifolder;
import org.eclipse.core.resources.iproject;
import org.eclipse.core.resources.iprojectdescription;
import org.eclipse.core.resources.iworkspaceroot;
import org.eclipse.core.resources.resourcesplugin;
import org.eclipse.core.runtime.coreexception;
import org.eclipse.jdt.core.iclasspathentry;
import org.eclipse.jdt.core.icompilationunit;
import org.eclipse.jdt.core.ijavaproject;
import org.eclipse.jdt.core.ipackagefragment;
import org.eclipse.jdt.core.ipackagefragmentroot;
import org.eclipse.jdt.core.itype;
import org.eclipse.jdt.core.javacore;
import org.eclipse.jdt.core.javamodelexception;
import org.eclipse.jdt.launching.javaruntime;

add code to run method. the code ignores the try/catch statements, eclipse will ask you to add exception handling code.

// create a project with name "testjdt"
iworkspaceroot root = resourcesplugin.getworkspace().getroot();
iproject project = root.getproject("testjdt");
project.create(null);
project.open(null);
 
//set the java nature
iprojectdescription description = project.getdescription();
description.setnatureids(new string[] { javacore.nature_id });
 
//create the project
project.setdescription(description, null);
ijavaproject javaproject = javacore.create(project);
 
//set the build path
iclasspathentry[] buildpath = {
		javacore.newsourceentry(project.getfullpath().append("src")),
				javaruntime.getdefaultjrecontainerentry() };
 
javaproject.setrawclasspath(buildpath, project.getfullpath().append(
				"bin"), null);
 
//create folder by using resources package
ifolder folder = project.getfolder("src");
folder.create(true, true, null);
 
//add folder to java element
ipackagefragmentroot srcfolder = javaproject
				.getpackagefragmentroot(folder);
 
//create package fragment
ipackagefragment fragment = srcfolder.createpackagefragment(
		"com.programcreek", true, null);
 
//init code string and create compilation unit
string str = "package com.programcreek;" + "\n"
	+ "public class test  {" + "\n" + " private string name;"
	+ "\n" + "}";
 
		icompilationunit cu = fragment.createcompilationunit("test.java", str,
				false, null);
 
//create a field
itype type = cu.gettype("test");
 
type.createfield("private string age;", null, true, null);

when you trigger the action, the following project will be created.

2. access projects

if there are already projects in our work space, we can use java model to loop through each of them.

	public void run(iaction action) {
		// get the root of the workspace
		iworkspace workspace = resourcesplugin.getworkspace();
		iworkspaceroot root = workspace.getroot();
		// get all projects in the workspace
		iproject[] projects = root.getprojects();
		// loop over all projects
		for (iproject project : projects) {
			system.out.println(project.getname());
		}
 
	}

if we import some projects or create some, and click the menu item we created, the projects names will show up as follows.

3. dynamically load/import existing projects into workspace

in the previous step, we need manually import existing projects to work space. if the number is larger, this would not be applicable.

eclipse jdt provide functions to do this dynamically. now let’s see how to import a large number of existing projects into the work space. it does not copy files to the workspace root directory, but only point to the projects in the external directory. in the example, i use the flash drive to hold my open source projects. in this way, you can parse thousands of projects and get useful information you need without copying anything.

iworkspaceroot root= resourcesplugin.getworkspace().getroot();
 
final iworkspace workspace = resourcesplugin.getworkspace();
 
system.out.println("root" + root.getlocation().toosstring());
 
runnable runnable = new runnable() {
	public void run() {
		try {
			ipath projectdotprojectfile = new path("/media/flashx/testprojectimport" + "/.project");
			iprojectdescription projectdescription = workspace.loadprojectdescription(projectdotprojectfile);
			iproject project = workspace.getroot().getproject(projectdescription.getname());
			javacapabilityconfigurationpage.createproject(project, projectdescription.getlocationuri(),	null);
			//project.create(null);
		} catch (coreexception e) {
			e.printstacktrace();
		}
	}
};
 
// and now get the workbench to do the work
final iworkbench workbench = platformui.getworkbench();
workbench.getdisplay().syncexec(runnable);
 
 
iproject[] projects = root.getprojects();
 
for(iproject project: projects){
	system.out.println(project.getname());
}

what if the project we want to load does not contain a .project file? this is the complicated case, we need dynamically create all those projects by using its source code.

notes

when you practice the examples above, you may got error message like “the type org.eclipse.core.runtime.iadaptable cannot be resolved. it is indirectly referenced from required .class files”. the solution is adding org.eclipse.core.runtime through plug-in menifest editor. simply adding to build path will not work.

if you think this article is useful and want to read more, you can go to eclipse jdt tutorial series i wrote.





Eclipse

Opinions expressed by DZone contributors are their own.

Related

  • Debug Like a Pro in 2025: 10 New Eclipse Java Debugger Features to Enhance Your Productivity (With Spring Boot Examples)
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook