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:\programwork\jdk1.6.0_25
 
 

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

    d:
    cd D:\data\temp\jdk_debug\
    copy %JAVA_HOME%\src.zip D:\data\temp\jdk_debug\
    :: 先要安装下winrar 
    start winrar x src.zip src\
     
     

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

      cd D:\data\temp\jdk_debug\src
      dir /B /S /X *.java>file.txt
       
       

        开始编译jdk源码,编译的class存放到D:\data\temp\jdk_debug\classes目录下

        %JAVA_HOME%\bin\javac ^
        -verbose -nowarn -g  ^
        -J-Xms512m -J-Xmx1024m  ^
        -encoding utf-8 ^
        -bootclasspath  %JAVA_HOME%\jre\lib\rt.jar;%JAVA_HOME%\jre\lib\jce.jar;%JAVA_HOME%\jre\lib\jsse.jar;%JAVA_HOME%\jre\lib\resources.jar;%JAVA_HOME%\jre\lib\charsets.jar;%JAVA_HOME%\jre\lib\deploy.jar  ^
        -sourcepath D:\data\temp\jdk_debug\src  ^
        -classpath D:\data\temp\jdk_debug\src  ^
        -d D:\data\temp\jdk_debug\classes  ^
        @file.txt  > log.txt 2>&
        

        确认编译后的class文件

        cd D:\data\temp\jdk_debug\classes
        dir
         驱动器 D 中的卷是 新加卷
         卷的序列号是 4EAC-5950
         D:\data\temp\jdk_debug\classes 的目录
        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
         
         

        找到%JAVA_HOME%\jre\lib\rt.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%\bin\javac…有45个类编译始终报错,有45个错误,错误示示例:
        D:\data\temp\jdk_debug\src\com\sun\java\swing\plaf\gtk\GTKLookAndFeel.java:45: 错误: 找不到符号
        import sun.awt.UNIXToolkit;
        ^
        符号: 类 UNIXToolkit
        位置: 程序包 sun.awt

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

        再次执行javac命令即可
        %JAVA_HOME%\bin\javac ^
        -verbose -nowarn -g ^
        -J-Xms512m -J-Xmx1024m ^
        -encoding utf-8 ^
        -bootclasspath %JAVA_HOME%\jre\lib\rt.jar;%JAVA_HOME%\jre\lib\jce.jar;%JAVA_HOME%\jre\lib\jsse.jar;%JAVA_HOME%\jre\lib\resources.jar;%JAVA_HOME%\jre\lib\charsets.jar;%JAVA_HOME%\jre\lib\deploy.jar ^
        -sourcepath D:\data\temp\jdk_debug\src ^
        -classpath D:\data\temp\jdk_debug\src ^
        -d D:\data\temp\jdk_debug\classes ^
        @file.txt > log.txt 2>&1

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

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

        编译前
        这里写图片描述

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

        • 0
          点赞
        • 0
          收藏
          觉得还不错? 一键收藏
        • 0
          评论

        “相关推荐”对你有帮助么?

        • 非常没帮助
        • 没帮助
        • 一般
        • 有帮助
        • 非常有帮助
        提交
        评论
        添加红包

        请填写红包祝福语或标题

        红包个数最小为10个

        红包金额最低5元

        当前余额3.43前往充值 >
        需支付:10.00
        成就一亿技术人!
        领取后你会自动成为博主和红包主的粉丝 规则
        hope_wisdom
        发出的红包
        实付
        使用余额支付
        点击重新获取
        扫码支付
        钱包余额 0

        抵扣说明:

        1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
        2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

        余额充值