java debug jdk(转载)

Debug info unavailable 解决之道

从事Java的小伙伴们估计都有断点代码的习惯,可以很方便的查看运行期代码中一些变量的值。
但是JDK中有些类你会发现是无法断点的,即使你在IDE中关联了src.zip依然不好使。这是为什么呢?

答案:

Java classes which are part of the JDK are compiled without debug info for the size and performance reasons. If you want debug info in these classes, you’ll either need to install a development version of the JDK where the classes are built with the debug info or rebuild the parts of JDK you want to debug from source with the debug info enabled and configure the new JDK with these versions of classes in jars.
This thread provides the instructions how to rebuild JDK classes in rt.jar from the source code with debugging information.
P.S. This question is not specific to IntelliJ IDEA.

翻译如下
由于大小和性能的原因,作为JDK一部分的Java类被编译时没有调试信息。 如果您需要这些类中的调试信息,则需要安装开发版本的JDK,其中类是使用调试信息构建的,或者在启用调试信息的情况下重新构建要从源代码调试的JDK的各个部分 带有这些版本的jar的新JDK。
此线程提供了有关如何使用调试信息从源代码重建rt.jar中的JDK类的说明。
附: 这个问题不是特定于IntelliJ IDEA的。

下面给出几种解决办法(推荐方法3,开发工程师建议自己都照着实战下,相信你会有收获的)

1.使用Intellij idea 13及以上版本

idea13及以上会有一个变量表,即使在没有编译类的调试信息时也会显示局部变量。

官网说明:
Show Local Variables in Debugger even with no Debug Info
Posted on October 17, 2013 by Andrey Cheptsov
December is just around the corner, and this means IntelliJ IDEA 13 is coming—with many cool new features and enhancements! One of these enhancements is the new debugger feature which shows local variables even when there is no debug information for the compiled classes. This is especially helpful when debugging third-party libraries or JDK sources compiled without corresponding debug information.

To test the new feature let’s go to Settings → Compiler → Java Compiler and turn off Generate debugging info (don’t forget to restore it afterwards):

官网介绍链接:
https://blog.jetbrains.com/idea/2013/10/show-local-variables-in-debugger-even-with-no-debug-info/

效果图:
这里写图片描述

2.下载使用带有debug信息的JDK。

找到这个下载地址也是费了一番周折的,为了让其它人少走冤枉路,直接贡献出来吧
http://download.java.net/jdk6/6u27/promoted/b03/binaries/jdk-6u27-ea-bin-b03-windows-i586-debug-27_may_2011.jar
PS:这种方式只能下载和特定版本号的,另外下载地址经常变,建议使用第3种方案

3.编译自己的JDK

准备工作:

set JAVA_HOME=D:programworkjdk1.6.0_25
  • 1

复制%JAVA_HOME%src.zip到D:data empjdk_debug并解压到src目录

d:
cd D:data	empjdk_debug
copy %JAVA_HOME%src.zip D:data	empjdk_debug
:: 先要安装下winrar 
start winrar x src.zip src
  • 1
  • 2
  • 3
  • 4
  • 5

将要重新编译的java文件记录到file.txt中

cd D:data	empjdk_debugsrc
dir /B /S /X *.java>file.txt
  • 1
  • 2

开始编译jdk源码,编译的class存放到D:data empjdk_debugclasses目录下

%JAVA_HOME%injavac ^
-verbose -nowarn -g  ^
-J-Xms512m -J-Xmx1024m  ^
-encoding utf-8 ^
-bootclasspath  %JAVA_HOME%jrelib
t.jar;%JAVA_HOME%jrelibjce.jar;%JAVA_HOME%jrelibjsse.jar;%JAVA_HOME%jrelib
esources.jar;%JAVA_HOME%jrelibcharsets.jar;%JAVA_HOME%jrelibdeploy.jar  ^
-sourcepath D:data	empjdk_debugsrc  ^
-classpath D:data	empjdk_debugsrc  ^
-d D:data	empjdk_debugclasses  ^
@file.txt  > log.txt 2>&1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

确认编译后的class文件

cd D:data	empjdk_debugclasses
dir
 驱动器 D 中的卷是 新加卷
 卷的序列号是 4EAC-5950
 D:data	empjdk_debugclasses 的目录
2018/01/16  11:30    <DIR>          .
2018/01/16  11:30    <DIR>          ..
2018/01/16  11:30    <DIR>          com
2018/01/16  11:30    <DIR>          java
2018/01/16  11:30    <DIR>          javax
2018/01/16  11:30    <DIR>          org
2018/01/16  11:30    <DIR>          sunw
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

找到%JAVA_HOME%jrelib t.jar文件(先备份一下),用winrar打开,将刚才编译产生的classes文件替换掉原来rt.jar中的即可
这里写图片描述

然后再在idea中进行断点即可看到效果了
这里写图片描述

参考文章:
https://stackoverflow.com/questions/1539282/intellij-debug-info-unavailable-how-to-fix
https://stackoverflow.com/questions/1313922/step-through-jdk-source-code-in-intellij-idea
https://stackoverflow.com/questions/1313922/step-through-jdk-source-code-in-intellij-idea/1313928#1313928?newreg=523c47ae584e400185929a87f267f17f


其它版本测试时遇到的问题

jdk7
执行%JAVA_HOME%injavac…有45个类编译始终报错,有45个错误,错误示示例:
D:data empjdk_debugsrccomsunjavaswingplafgtkGTKLookAndFeel.java:45: 错误: 找不到符号
import sun.awt.UNIXToolkit;
^
符号: 类 UNIXToolkit
位置: 程序包 sun.awt

遇到这种情况建议删除D:data empjdk_debugsrc下面的comsun目录然后重新编译就可以成功了
rd /s/q D:data empjdk_debugsrccomsun
dir /B /S /X D:data empjdk_debug*.java>file.txt

再次执行javac命令即可
%JAVA_HOME%injavac ^
-verbose -nowarn -g ^
-J-Xms512m -J-Xmx1024m ^
-encoding utf-8 ^
-bootclasspath %JAVA_HOME%jrelib t.jar;%JAVA_HOME%jrelibjce.jar;%JAVA_HOME%jrelibjsse.jar;%JAVA_HOME%jrelib esources.jar;%JAVA_HOME%jrelibcharsets.jar;%JAVA_HOME%jrelibdeploy.jar ^
-sourcepath D:data empjdk_debugsrc ^
-classpath D:data empjdk_debugsrc ^
-d D:data empjdk_debugclasses ^
@file.txt > log.txt 2>&1

jdk8
问题同jdk7,解决办法也同jdk7

重新编译jdk源码后的对比图

编译前
这里写图片描述

编译后,细心的你一定会发现,鼠标放置到变量上会显示具体的值的,另外在下方的variables面板中可以看到各个变量运行期的值的
这里写图片描述

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hl_java/article/details/79078886
原文地址:https://www.cnblogs.com/diyunpeng/p/8641012.html