How to See difference between two Files in Eclipse - Text Comparision

One of the common task for every programmer is is to compare two files and find out difference between them. You would do this while comparing same file from different release version or from different environment to find out exactly what has changed. Though there are 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 of 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 from long time, but I have practising 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 lead me to find the option I am going to share you with you guys here.

I was very surprised that, even though I was using text comparison feature of Eclipse to see difference between local file with 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 developer 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.

How to see difference between two files in Eclipse

If you have been using Eclipse source control plugins for SVN and CVS then you must be familiar with "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 motivate 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 difference 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 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 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 developers need of writing, running and debugging code. May be I can learn few more tricks from you guys too. 

1 comment :

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.

Post a Comment