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
  • Understanding and Learning NoSQL Databases With Java: Three Key Benefits
  • Achieving Inheritance in NoSQL Databases With Java Using Eclipse JNoSQL
  • From J2EE to Jakarta EE

Trending

  • It’s Not About Control — It’s About Collaboration Between Architecture and Security
  • The Modern Data Stack Is Overrated — Here’s What Works
  • Understanding Java Signals
  • A Guide to Developing Large Language Models Part 1: Pretraining
  1. DZone
  2. Coding
  3. Frameworks
  4. How to Format Java Code Using Eclipse JDT?

How to Format Java Code Using Eclipse JDT?

By 
Ryan Wang user avatar
Ryan Wang
·
Apr. 22, 13 · Interview
Likes (0)
Comment
Save
Tweet
Share
6.4K Views

Join the DZone community and get the full member experience.

Join For Free

Yo probably format your code often by pressing Ctrl+Shift+F or right clicking Source -> Format. This function is also provide in JDT, so you can also format your Java code in code.

However finding correct class to do this function is not straight-forward, because one of them is a internal class.

The following is the code to format Java code by using DefaultCodeFormatter.

import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.formatter.CodeFormatter;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.TextEdit;
 
public class FormatterTest {
 
	public static void main(String[] args) {
		String code = "public class TestFormatter{public static void main(String[] args){System.out.println(\"Hello World\");}}";
		CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(null);
 
		TextEdit textEdit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, code, 0, code.length(), 0, null);
		IDocument doc = new Document(code);
		try {
			textEdit.apply(doc);
			System.out.println(doc.get());
		} catch (MalformedTreeException e) {
			e.printStackTrace();
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}
}

he apply() method in TextEdit class is the key to this problem. It applies the edit tree rooted by this edit to the GIVEN document.

Output in console:

JDT-codeformatter






Depending on your Eclipse version, you will need the following jar files:

  1. org.eclipse.core.contenttype_3.4.1.R35x_v20090826-0451.jar
  2. org.eclipse.core.jobs_3.4.100.v20090429-1800.jar
  3. org.eclipse.core.resources_3.5.2.R35x_v20091203-1235.jar
  4. org.eclipse.equinox.common_3.5.1.R35x_v20090807-1100.jar
  5. org.eclipse.equinox.preferences_3.2.301.R35x_v20091117.jar
  6. org.eclipse.jdt.core_3.5.2.v_981_R35x.jar
  7. org.eclipse.osgi_3.5.2.R35x_v20100126.jar
  8. org.eclipse.text_3.5.101.v20110928-1504.jar
  9. org.eclipse.core.runtime_3.5.0.v20090525.jar

 

Java (programming language) Eclipse

Published at DZone with permission of Ryan Wang. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Simplify NoSQL Database Integration in Java With Eclipse JNoSQL 1.1.3
  • Understanding and Learning NoSQL Databases With Java: Three Key Benefits
  • Achieving Inheritance in NoSQL Databases With Java Using Eclipse JNoSQL
  • From J2EE to Jakarta EE

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!