Few days back I got stuck into situation , in which I needed to debug Java sdk code . I found few websites on goggling but none of the gave perfect solution . So after struggling for few more days got a way to do it an thought would be useful to blog it out .
The ingredients required for this work are pretty basic ,
- Eclipse (preferably indigo but it doesn't matter for sure)
- Java SDK (preferably 6 or above) . Mostly Java SDK comes with a source code else you could download it from Oracle website.
Next Step would be to create a dummy Application , I will call it FileLoadingExample .
Creation of Projects |
As you can see in the above screen shot , I have a class with name FileLoadingFromClassLoader with our main method. In this method I am trying to load a file using class loader .
My requirement is to debug getResourceAsStream method of Class loader .
So lets dig in
There are basically 2 steps in it
- Import downloaded Java source code : Now this would not be straight forward as JDK source code might not come with eclipse configuration files (There are some sources from where you can get source code with .project files which can be directly used in eclipse from Import utility). Lets assume you don't have code in eclipse format , then create a dummy project as shown in above screen shot (JavaSourceCodeProject) and copy all the source code from source of JDK to src folder of this project i.e. packages copied would be com,java,javax,launcher,org,sunw and others (if any). After building project , you might find some errors , try resolving it and if you are not able to resolve it , don't worry much as this wouldst be a show stopper.
Once this copying is done next step would be to include this project as Referenced Project for our
sample project under Projects Tab and modify Order and Export tab ( our imported java code
java code project should be above JRE )
Add Java Code project as Reference Project |
Ordering of projects for sample project . (Notice our Java Code project comes above JRE) |
Now after this step , we would be in position to go to corresponding java class in JDK source
code after we click on needed method (standard ctrl + Mouse click used in eclipse ) .
So now I ctrl + Mouse Click on method getResourceAsStream which takes me to class
ClassLoader.java and I put a break point at first line of the method.
2. In the second Step , we need to change some debugging configuration for our project. In this step
, we need to go to debugging Configuration of our sample project .
On Click of this , it would take us to debugging configuration screen and then we need to
go to Class Path Tab . In this tab you need to add our Java Source project under Bootstrap
entries .
Remember : Our Java Source Project should be the first project in Bootstrap Entries
above JRE System library
Going to Debug Configuration |
Adding Java Code Project to Boot Strap Entries |
Now debug your sample class using eclipse debug and put break point where ever you want in the
java code and your debug would stop at that point and further there , you could inspect variable
and dig more deep if needed . As you might have error in you java project , it would give you alert
box asking do we need to proceed or not , just go ahead it should be fine
Debugging java Class Loader Class |
No comments:
Post a Comment