Compiling JDK classes with debug enabled

21.12.2014 by Dirk Olmes

I recently had to poke around with the JAAS classes in the JDK and wanted to step through with a debugger. But the JDK ships with a rt.jar that’s not compiled with debugging symbols enabled. Thus, debugging into the JDK sources is not much of a success.

Some googling suggests that recompiling the relevant parts of the JDK with debug enabled is the way to go. Some different approaches are mentioned out on the ‘net but I’d like to share what worked best for me.

First, unpack the src.zip found inside the JDK into some directory. At first, I blindly tried to recompile all of the JDK sources but the compiler failed with a lot of error messages and I was too lazy to dig deeper about those failures.

Just list all the available java source files e.g. using the unix find command:

find . -name *.java -print > java-files.txt

Now I used grep to extract only the relevant classes to recompile, e.g.

grep './javax/security' java-files.txt > security.txt
grep './com/sun/security/' java-files.txt >> security.txt

Now we can run the compiler using security.txt as an input file:

CLASSPATH=${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/tools.jar
javac -g -J-Xmx512m -cp "$CLASSPATH" @security.txt

After successful compilation the class files reside next to the source files in your unpack folder.

For the ease of debugging, just configure your IDE to put the directory containing the recompiled JDK classes first on the boot classpath. This way, the classes you recompiled will shadow the corresponding classes from the JDK’s rt.jar while all the other classes you did not recompile will be loaded regularly from the rt.jar.

That’s it - enjoy debugging your JDK.


Comments

There are no comments yet.

Leave a comment
Your name:
Comment: