fkn
fkn

Reputation: 567

Debug classes in rt.jar by copying their source to my project

I just found that there is an easy way to debug/hack third-party libraries that I use in my project.

For example if I want to debug org.springframework.beans.factory.support.AbstractBeanDefinition from spring-beans all I need to do is to create a copy of this class in my project in the same package with the same source. As my project sources happen to appear earlier in the class path the ClassLoader will use them instead of the ones from spring-beans. Then I can add logging/breakpoints/tweaks to the standard class behavior as the source now is under my control.

Howere this doesn't work with standard Java classes located in rt.jar. For example it doesn't work with java.util.HashMap. Is there any way to use the same approach with rt.jar classes?

Upvotes: 1

Views: 199

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122364

You would have to put your replacement classes in a separate directory or JAR file and prepend that to the bootstrap classpath using the -Xbootclasspath/p option to the java command. You should not distribute any code that uses this mechanism to override rt.jar classes but it's fine to use for debugging purposes.

Note that you should only use the bootstrap classpath for things they really need to be there (the specific class files that replace classes from rt.jar). The rest of your application should stay on the normal classpath to be loaded by the system classloader, as there may be other loaders in between the bootstrap and system loaders (e.g. for extensions) that your code wouldn't be able to use if it were on the bootstrap classpath.

Upvotes: 1

Related Questions