Wednesday, February 5, 2014

How to attach source in eclipse for Jars, debugging and code look-up – JDK Example

Attaching source of any Jar in Eclipse e.g. JDK or open source libraries like Spring framework is good idea because it help during debugging and code development. As a Java programmer at least you should attach source of JDK in Eclipse IDE to find out more about JDK classes. Though Eclipse IDE is pretty good on code assist, sometime You want to know what's going inside a library or a JDK method, rather than just reading documentation. Interview questions like How HashMap works in Java or How substring cause memory leak can only be answered if you are familiar with source code of these classes.  Once you attach source code of Java or Spring in Eclipse IDE, You can check code with just a single click. Some one may argue for Java decompiler like JAD which can create source from .class file which is also a smart way to look code for open source library, but decompiled source file is not same as original source file, as you lost comment and readability. As per my experience attaching source code corresponding to JAR file is much better than decompiling a class file using 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 is not available if you are decompiling class file. But at same time its not possible to attach Java source code of every single library in Eclipse. So combination of both decompiler and source code attachment in Eclipse is 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 source code of JDK by following steps given in this tutorial.

Attaching source code of JDK in Eclipse IDE

There are multiple ways to attach source in Eclipse for any JAR file, but I found following approach as most simple one because it attach code directly to the JAR file by right clicking ot 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 source path.

After performing above 4 steps following window will appear where you can browse source code for any JAR file. In our case source code for rt.jar is src.zip which can be found in JAVA_HOME directory or JDK installation directory in 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 code of HashSet classto find out How HashSet works in Java or source code of java.lang.String class to find out 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 displayjava.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 above steps, select java.lang.String again. This time you will see proper source for String class from rt.jar as shown in 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 purpose. I personally add source code  of JDK and frequently used library like Spring, Hibernate or Apache commons and leave rest of JAR for 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

4 comments:

oakleyses said...

jordan pas cher, chanel handbags, nike outlet, michael kors pas cher, kate spade outlet, replica watches, longchamp pas cher, nike free, jordan shoes, christian louboutin shoes, nike free run, louis vuitton outlet, oakley sunglasses, ray ban sunglasses, polo ralph lauren, ugg boots, christian louboutin uk, air max, louis vuitton outlet, ugg boots, tiffany jewelry, polo ralph lauren outlet online, burberry pas cher, prada outlet, nike air max, gucci handbags, sac longchamp pas cher, ray ban sunglasses, louboutin pas cher, louis vuitton, uggs on sale, tiffany and co, oakley sunglasses, louis vuitton outlet, ray ban sunglasses, longchamp outlet, louis vuitton, longchamp outlet, replica watches, nike roshe, polo outlet, oakley sunglasses, cheap oakley sunglasses, oakley sunglasses wholesale, christian louboutin, christian louboutin outlet, tory burch outlet

oakleyses said...

supra shoes, marc jacobs, canada goose, converse outlet, toms shoes, ugg uk, ugg pas cher, canada goose outlet, canada goose, nike air max, louis vuitton, karen millen uk, doudoune moncler, converse, louis vuitton, vans, gucci, montre pas cher, moncler outlet, canada goose uk, canada goose outlet, moncler, pandora jewelry, barbour uk, moncler outlet, juicy couture outlet, louis vuitton, canada goose, pandora uk, coach outlet, swarovski crystal, ugg,uggs,uggs canada, juicy couture outlet, links of london, louis vuitton, louis vuitton, hollister, swarovski, canada goose outlet, ray ban, moncler, ugg, barbour, ugg,ugg australia,ugg italia, replica watches, pandora charms, moncler uk, pandora jewelry, wedding dresses, canada goose jackets, hollister, lancel

yanmaneee said...

michael kors outlet online
fila disruptor
coach handbags
michael kors outlet
jordans
nike air vapormax
goyard handbags
hermes belt
nfl store
kobe byrant shoes

Anonymous said...

cheap replica handbags CMQW replica designer bags EGPU high quality designer replica GFRU