您的位置:首页 > 编程语言 > Java开发

在eclipse中调试jdk源码

2014-06-30 18:33 330 查看
1、在eclipse中调试源代码

提示错误:今天想要调试下jdk的源码,结果提示Unable to install breakpoint in javax.xml.parsers.XXX due to missing line number attributes.Modify compiler options to generate line number attributes. Reason:Absent Line Number Information

解决办法:修改eclipse中使用的jre

window-->preferences-->Installed JREs



这里使用的jre,要想调试jdk源码,需要修改下Installed JREs为jdk



点击OK,然后选中项目,build path,先移除原有jre,然后重新添加jre,默认都会自动选择刚修改的jdk,然后就OK了

参考文章:http://blog.csdn.net/xuefeng0707/article/details/8738869

2、虽然现在可以调试源码了,但是debug时,却发现无法查看变量的值

原因:因为在jdk中,sun对rt.jar中的类编译时,去除了调试信息,这样在eclipse中就不能看到局部变量的值。这样的话,如果在debug的时候查看局部变量,就必须自己编译相应的源码使之拥有调试信息。要达到这个目的,一是找网上人家已经编译好的版本,剩下的只能自己去编译。下面是自己编译的方法:

Create your working folder. I chose
d:\
root folder (创建一个工作目录,比如d:\root)

Inside your working folder create the source folder i.e.
jdk7_src
and output folder
jdk_debug(在工作目录下,创建2个文件夹,比如jdk7_src和jdk_debug)

From your
JDK_HOME
folder get the
src.zip
file and unzip it inside
jdk7_src(从你的jdk_home目录下将src.zip拷贝并解压到jdk7_src目录)

Select what you will compile and delete the rest. For all of them you might need additional steps. I have chosen the folders:(选择你将要编译的文件夹,并且删掉其他几个文件夹。因为如果全选的话,你可能需要额外的步骤,这里我选择以下文件夹)

java

javax

org


From your
JDK_HOME\jre\lib
get the file
rt.jar
and put in the work folder (this is only for convenience to not specify too large file names in the command line).(从你的
JDK_HOME\jre\lib
目录下找到rt.jar,并且将他放在工作目录下,即d:\root目录下(这步只是为了在dos下执行命令时,不用指定太多的文件名,更方便些))
Execute the command:
dir /B /S /X jdk7_src\*.java > filelist.txt
to create a file named
filelist.txt
with the list of all java files that will be compiled. This will be given as input to
javac(执行
dir
/B /S /X jdk7_src\*.java > filelist.txt
,该命令会创建一个名为filelist.txt的文件,该文件列出了将要编译的所有java文件,作为执行javac命令的输入参数)

Execute
javac
using the command:

javac -J-Xms16m -J-Xmx1024m -sourcepath d:\jdk7_src -cp d:\rt.jar -d d:\jdk_debug -g @filelist.txt >> log.txt 2>&1
This will compile all the files in the
jdk_debug
folder and will generate a
log.txt
file in your working folder. Check the log contents. You should get a bunch of warnings but no error.(用javac执行如下命令:javac -J-Xms...log.txt 2>&1;执行之后,会将所有编译后的文件生成在jdk_debug目录下,同时会在工作目录下生成一个log.txt文件。检视文件的内容,你应该会看到很多警告,但是不会有错误)
Go inside the
jdk_debug
folder and run the command:
jar cf0 rt_debug.jar *
. This will generate your new runtime library with degug information.(到jdk_debug目录中去,并且执行
jar cf0 rt_debug.jar *
,它将会生成一个新的带有调试信息的运行时lib文件,命名为rt_debug.jar包)
Copy that new jar to the folder
JDK_HOME\jre\lib\endorsed
. If the
endorsed
folder does not exist, create it.(拷贝这个新的jar包到
JDK_HOME\jre\lib\endorsed
这个目录,如果endorsed无目录不存在,那么自己创建)
如果你是在eclipse中debug的。点击Window->Installed JRES,选择相应的JDK,点击Edit,r然后选择点击Add External jars,选择我们步骤9中目中的rt_debug.jar,就可以了。现在完成了所有的步骤了,赶快尝试debug一下,如果可以查看局部变量了,那么恭喜你成功了。如果还不行,请讲刚刚加入的rt_debug.jar包在上一步中up到最上面,默认Add External jars是加到最后面的

参考文章:http://blog.csdn.net/appleprince88/article/details/21873807
http://stackoverflow.com/questions/18255474/debug-jdk-source-cant-watch-variable-what-it-is



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: