Tuesday, August 3, 2021

How to see difference between two Files in Eclipse - Text Comparison Example Tutorial

One of the common tasks for every programmer is to compare two files and find out the difference between them. You would do this while comparing the same file from different release versions or from the different environments to find out exactly what has changed. Though there are a lot of good tools already exists to compare two files e.g. diff command in UNIX, Win Merge, and Beyond compare in Windows, they are external tools. Beyond compare is not even free, but to be frank it's worth money. I would like to compare two files directly from Eclipse to avoid switching to another program for a simple task.

Actually, I have been using Beyond compare for a long time, but I have practicing to do every task ( which I can ) from Eclipse to save time by avoiding transition between multiple applications.

This leads me to find out how do I compare two files in Eclipse, it could be Java source files, text files, or simply .properties or XML files. This search leads me to find the option I am going to share with you guys here.

I was very surprised that, even though I was using the text comparison feature of Eclipse to see the difference between a local file with a remote file in CVS, SVN, TFS, and other source control systems in Eclipse for more than 5 to 6 years, I had missed this important feature to compare two totally unrelated files.

This is a very good example of how much Java developers know about the IDE they use every day. There are many such features, and I am sure you guys can share something similar too.

By using this "compare with each other option" you can just select two files and compare it right there in Eclipse itself.

Btw, if you are a beginner, I suggest you first go through a beginner Eclipse course to understand the core concepts of Eclipse IDE and get yourself familiar with UI and essential features. Learning plugins will be a lot easier after that.


How to see the difference between two files in Eclipse? Steps

If you have been using Eclipse source control plugins for SVN and CVS then you must be familiar with the "compare with" option, which comes when you right click on any file. I was this option to compare current file with remote file in SVN, or any particular version of same file.


This option can also be used to compare two files, which are not under control of any source control. All you need to do is just select those two files and right click, select "compare with", this time it will show one more option "Each Other". Use this option, it will allow you to compare file side-by-side in Eclipse IDE itself, similar to UNIX diff --side-by-side command.

By the way, comparison screen is the same one which is used to compare files on CVS and SVN, which means you are already familiar with that. here is the screen shot, which will show you how exactly you can compare two files in Eclipse and see difference between them in just one click.


Steps to compare Two Files in Eclipse

To compare two files in Eclipse, select both files (Control click them) and in the contextual menu (right button), chose Compare With > Each Other. By the way, If the option "Each Other" doesn't appear in the menu "Compare with", go to Preferences > Capabilities > Advanced and enable Team > Core Team Support, then restart Eclipse. A picture is worth of thousand words. Here is the screenshot to compare two XML file in Eclipse. We are comparing two versions of pom.xml here :

How to compare two files in Eclipse IDE

and here is the output of text comparison in Eclipse :

How to see differences between two files in Eclipse


This discovery actually motivates me to continue my run of discovering full potential of Eclipse IDE. Right now, I use Eclipse as for writing Java code, running and debugging Java program, running unit tests, running DOS commands from Eclipse, seeing differences between different version of same file under source control e.g. SVN, and CVS. Using it as XML editor, by using its collapse/expand feature to view large XML files, viewing HTML files, using eclipse browser to see JIRA, confluence, Jenkins and Java docs, spell checker, viewing source code of JAR files and now using it compare two arbitrary files.

I am doing a couple of more things in Eclipse, which I am not able to recall now, but will post it as and when it comes to my mind. My goal is to use Eclipse as much as possible, just to avoid the need of many application and saving time by not switching between them. Let me know how you guys are using Eclipse apart from common Java developer's need of writing, running and debugging code. Maybe I can learn few more tricks from you guys too. 


Other Java Eclipse articles you may like to explore
  • 30 Useful Eclipse Shortcuts for Java Developers (list)
  • How to remote debug Java application in Eclipse? (tutorial)
  • 10 Eclipse debugging tips Java developer should know? (see here)
  • How to attach source code for JAR file in Eclipse? (guide)
  • Eclipse shortcut to print System.out.println statements? (shortcut)
  • How to increase console buffer size in Eclipse? (steps)
  • How to use spaces instead of tabs in Eclipse? (guide)
  • How to create an executable JAR file from Eclipse? (example)
  • 3 Books to Learn Eclipse IDE for Java developers (list)
  • How to Increase Heap Size of Java Program running in Eclipse? (guide)

Thanks for reading this article so far. If you like this article then please share with your friends and colleagues. If you have any questions or feedback then please drop a comment.

2 comments :

Anonymous said...

Interesting tip, I even didn't know about this. I like the idea of using Eclipse as single tool to do this kind of things, it doesn't make sense to start and learn new application for day to day programming tasks.

Alex Turner said...

At the moment I am using a free utility designed to compare and merge different files and folders https://www.devart.com/codecompare/compare-folders.html You need to simultaneously open two files and compare their lines of code with this lightweight application.

Post a Comment