简体   繁体   English

通过将rt.jar中的类复制到我的项目中来调试它们

[英]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. 例如,如果我想从spring-beans调试org.springframework.beans.factory.support.AbstractBeanDefinition ,我要做的就是在同一项目中的同一包中使用相同的源创建此类的副本。 As my project sources happen to appear earlier in the class path the ClassLoader will use them instead of the ones from spring-beans. 当我的项目源恰好出现在类路径中的较早位置时, ClassLoader将使用它们而不是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. 但是,这不适用于rt.jar中的标准Java类。 For example it doesn't work with java.util.HashMap . 例如,它不适用于java.util.HashMap Is there any way to use the same approach with rt.jar classes? 有什么办法可以对rt.jar类使用相同的方法吗?

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. 您将必须将替换类放在单独的目录或JAR文件中,并使用java命令的-Xbootclasspath/p选项将其放在引导类路径之前。 You should not distribute any code that uses this mechanism to override rt.jar classes but it's fine to use for debugging purposes. 您不应分发任何使用此机制来覆盖rt.jar类的代码,但可以将其用于调试目的。

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). 请注意,您仅应将bootstrap类路径用于确实需要它们的东西(用于替换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 (eg for extensions) that your code wouldn't be able to use if it were on the bootstrap classpath. 您的应用程序的其余部分应保留在系统类加载器要加载的普通类路径上,因为在引导程序和系统加载器之间可能还有其他加载器(例如扩展),如果您的代码被使用,则无法使用。在引导类路径上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM