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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Simplify NoSQL Database Integration in Java With Eclipse JNoSQL 1.1.3
  • Domain-Driven Design: Manage Data With Jakarta Data and JNoSQL
  • Understanding and Learning NoSQL Databases With Java: Three Key Benefits
  • Achieving Inheritance in NoSQL Databases With Java Using Eclipse JNoSQL

Trending

  • From Zero to Production: Best Practices for Scaling LLMs in the Enterprise
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 1
  • Why High-Performance AI/ML Is Essential in Modern Cybersecurity
  • Building Enterprise-Ready Landing Zones: Beyond the Initial Setup
  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 user avatar
Ryan Wang
·
Mar. 21, 13 · Interview
Likes (0)
Comment
Save
Tweet
Share
8.0K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Simplify NoSQL Database Integration in Java With Eclipse JNoSQL 1.1.3
  • Domain-Driven Design: Manage Data With Jakarta Data and JNoSQL
  • Understanding and Learning NoSQL Databases With Java: Three Key Benefits
  • Achieving Inheritance in NoSQL Databases With Java Using Eclipse JNoSQL

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!