Tuesday, August 24, 2021

How to Attach Source Code in Eclipse to JAR Files for Debugging and Navigation With JDK Example

Attaching the source of any Jar in Eclipse e.g. JDK or open-source libraries like Spring framework is a good idea because it helps during debugging and code development. As a Java programmer at least you should attach the source of JDK in Eclipse IDE to find out more about JDK classes. Though Eclipse IDE is pretty good on code assist, sometimes You want to know what's going inside a library or a JDK method, rather than just reading the documentation. Interview questions like How HashMap works in Java or How substring causes memory leak can only be answered if you are familiar with the source code of these classes.  

Once you attach the source code of Java or Spring in Eclipse IDE, You can check the code with just a single click. Someone may argue for a Java decompiler like JAD which can create a source from the .class file which is also a smart way to look code for an open-source library, but the decompiled source file is not the same as the original source file, as you lost comment and readability.

As per my experience attaching source code corresponding to the JAR file is much better than decompiling a class file using the JAD decompiler. 

As I said when you attach source code for any JAR file, You not only see properly formatted code but also all the code comments which are not available if you are decompiling the class file. But at the same time, it's not possible to attach the Java source code of every single library in Eclipse. So the combination of both decompiler and source code attachment in Eclipse is the best way to go. 

As I have always argued for learning Eclipse keyboard shortcuts and  Eclipse settings to improve productivity e.g. save action, this is also one of the Eclipse tips which will help you during development and debugging in Java. In this Eclipse tutorial, we will see How to attach source code for any JAR in Eclipse and we will attach the source code of JDK by following the steps given in this tutorial.

Btw, if you are a beginner, I suggest you first go through a beginner course like Eclipse Tutorials for Beginners 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.



Attaching source code of JDK in Eclipse IDE

There are multiple ways to attach a source in Eclipse for any JAR file, but I found the following approach as the most simple one because it attaches code directly to the JAR file by right-clicking on it.

1. Select any Java project
2. Expand Referenced libraries
3. Select any JAR file, in our case rt.jar which is Java runtime
4. Right-click and go to properties
5. Attach source code by browsing the source path.

After performing the above 4 steps following window will appear where you can browse the source code for any JAR file. In our case source code for rt.jar is src.zip which can be found in the JAVA_HOME directory or JDK installation directory on your computer.

How to attach source in Eclipse - step by step guide


Now let’s see What difference attaching source code in Eclipse makes. Suppose you want to see the code of HashSet class to find out How HashSet works in Java or the source code of java.lang.String class to find out the internal implementation of String class. 

Let’s use Eclipse shortcut to find any Type e.g. ctrl+T and select java.lang.String class. If you don’t have source code attached for rt.jar or JDK, as java.lang.String belongs to rt.jar, it will display java.lang.String class in class file editor as shown below.


Attaching source in Eclipse for debugging Java JDK


Once you attach source code in Eclipse for JDK by following the above steps, select java.lang.String again. This time, you will see the proper source for the String class from rt.jar as shown in the following pic.

How to attache source in Eclipse for JAR for debugging

 
That’s all on How to attach source code in Eclipse for any JAR for debugging or code lookup purposes. I personally add source code of JDK and frequently used libraries like Spring, Hibernate, or Apache commons and leave the rest of JAR for the decompiler to take care of. You can attach source code for even your in-house libraries which is quite common in Investment banks.


Related Eclipse Tutorials for Java programmers

14 comments :

Anonymous said...

I usually attach source corresponding to any JAR or class file by clicking on any class from JAR e.g. java.lang.BigInteger, which will open class file in Class File Editor and says source not found " JAR file rt.jar has no source attachment" and gives an option to attach source. Just click on that button and attach source. Simple.

Unknown said...

Suppose if i want to see Code for ActionServlet class from strut-core-1.3.8 jar,how can will attach the source,i tried add that jar file ,but still i didn't got it

Anonymous said...

actually src.zip which is present inside java installation directory has all file as .java then while opening in Eclipse why it open as .class file?

Rushi Shah said...

Thanks a lot for your help! I can now see source cods of jdk internal classes in my eclipse.

Tatiana said...

Oh, thank you so much for this. I am new to Eclipse and couldn't figure out how to (re)attach my source code. This is so much better!

P.s vinoth said...

I want to debug the source code of String class from lang package of attached source, how i can debug that that, break point is not getting trig erred inside String class, can u help me?

Anonymous said...

Why not attach JDK source code to every project you create instead of a single project?
You can accomplish that using:
1) (Eclipse main menu) Window | Preferences;
2) Expand "Java" entry;
3) Select "Installed JREs";
4) In the right panel select the JRE of your choice (if you have more than one, of course) and
click Edit;
5) In "JRE system libraies" expand "\lib\rt.jar";
6) Select "Source attachment" and click "Source Attachment" button;
7) Select "External location" and click "External File...";
8) Locate src.zip file in your JDK installation and click "Open";

Right after "Source attachment" (6) you have "Javadoc location" in which you can attach your local
copy of java documentation instead of using the online one.

Another tip: if you use or plan to use JavaFX with Eclipse you can do the same as above with jfxrt.jar entry, located down the JARs list.

My $0.02 to the community.
Romualdo Rubens de Freitas (Brazil)

cunsalan said...

Thanks man.

Binh Nguyen said...

Thanks, nice tips

Unknown said...

Hi all ,
Do any one of you , how can i actualy debug the java source code i.e , how can i actually inspect or evaluate a variable in a java native class at run time .

Unknown said...

HOW TO attached the Spring .jar files to eclipse for seeing the internal code,
can you share the screen-sharts to my email: rameshpochana465@gmail.com

Anonymous said...

how can i add the source code of spring and hibernate. what zip file do i have to download and where can i find one

Nikhil said...

Great explanation and perfect content, this website is well designed and has good exposure but why still it is ".blogspot.com" and not ".com" ?

Anonymous said...

This blog was... how do I say it? Relevant!! Finally I've found something that helped me.
Many thanks!

Post a Comment