现在的位置: 首页 > 综合 > 正文

Java程序的调试运行与调试连接

2013年07月03日 ⁄ 综合 ⁄ 共 6749字 ⁄ 字号 评论关闭

转载于:http://liugang594.iteye.com/blog/1354426


ava里提供了非常强大、易用的调试工具和调试接口,可以方便的用来以调试的方式进行java应用,并且可以很容易的连接上这些调试。

 

这里简单介绍一下如何在指定的端口上以调试的方式启动一个程序,并且在另外一个命令中连接上这个程序进行调试。

 

整个Java平台调试框架(JavaTM Platform Debugger Architecture (JPDA))由三个部分组件:

 

 不过我不要是关心这个,我只介绍一下怎么使用Java调试工具:JDB (Java Debugger).

 

首先需要指定程序以调试的方式启动,命令行如下:

Java代码  收藏代码
  1. javaw.exe -Xdebug -agentlib:jdwp=transport=dt_socket,server=y,suspend=y|n,address=[host:]<port_number> -Dfile.encoding=<encoding> -classpath <classpath> <main_class>  

 

上面的意思是说:

在端口<port_number>上监听socket连接,并且在main类加载之前挂起虚拟机。一旦调试程序连接上了,再发出一个JDWP的命令唤醒虚拟机

 

 

更多的命令行指导,请参考:http://docs.oracle.com/javase/6/docs/technotes/guides/jpda/conninv.html

 

运行上面的命令,如果成功,会在console上打印一条类似如下消息:

Java代码  收藏代码
  1. Listening for transport dt_socket at address: 3556  

 

表明程序启动成功。

 

下面就可以用JDB去尝试连接调试了,命令行为:

Java代码  收藏代码
  1. jdb -sourcepath <source_dir1:source_dir2:...> -connect com.sun.jdi.SocketAttach:hostname=<host>,port=<port_number>  

 

更多的,请参考:http://docs.oracle.com/javase/1.3/docs/tooldocs/solaris/jdb.html

如果启动成功,则可以看到类似下面的消息:

Java代码  收藏代码
  1. Set uncaught java.lang.Throwable  
  2. Set deferred uncaught java.lang.Throwable  
  3. Initializing jdb ...  
  4. >  
  5. VM Started: No frames on the current call stack  
  6.   
  7. main[1]  

 

程序现在在主线程等待进一步的指示命令。

 

要查看帮助可以使用命令:help。 例如:

Java代码  收藏代码
  1. main[1] help  
  2. ** command list **  
  3. connectors                -- list available connectors and transports in this VM  
  4.   
  5. run [class [args]]        -- start execution of application's main class  
  6.   
  7. threads [threadgroup]     -- list threads  
  8. thread <thread id>        -- set default thread  
  9. suspend [thread id(s)]    -- suspend threads (default: all)  
  10. resume [thread id(s)]     -- resume threads (default: all)  
  11. where [<thread id> | all] -- dump a thread's stack  
  12. wherei [<thread id> | all]-- dump a thread's stack, with pc info  
  13. up [n frames]             -- move up a thread's stack  
  14. down [n frames]           -- move down a thread's stack  
  15. kill <thread id> <expr>   -- kill a thread with the given exception object  
  16. interrupt <thread id>     -- interrupt a thread  
  17.   
  18. print <expr>              -- print value of expression  
  19. dump <expr>               -- print all object information  
  20. eval <expr>               -- evaluate expression (same as print)  
  21. set <lvalue> = <expr>     -- assign new value to field/variable/array element  
  22. locals                    -- print all local variables in current stack frame  
  23.   
  24. classes                   -- list currently known classes  
  25. class <class id>          -- show details of named class  
  26. methods <class id>        -- list a class's methods  
  27. fields <class id>         -- list a class's fields  
  28.   
  29. threadgroups              -- list threadgroups  
  30. threadgroup <name>        -- set current threadgroup  
  31.   
  32. stop in <class id>.<method>[(argument_type,...)]  
  33.                           -- set a breakpoint in a method  
  34. stop at <class id>:<line> -- set a breakpoint at a line  
  35. clear <class id>.<method>[(argument_type,...)]  
  36.                           -- clear a breakpoint in a method  
  37. clear <class id>:<line>   -- clear a breakpoint at a line  
  38. clear                     -- list breakpoints  
  39. catch [uncaught|caught|all] <class id>|<class pattern>  
  40.                           -- break when specified exception occurs  
  41. ignore [uncaught|caught|all] <class id>|<class pattern>  
  42.                           -- cancel 'catch' for the specified exception  
  43. watch [access|all] <class id>.<field name>  
  44.                           -- watch access/modifications to a field  
  45. unwatch [access|all] <class id>.<field name>  
  46.                           -- discontinue watching access/modifications to a field  
  47. trace [go] methods [thread]  
  48.                           -- trace method entries and exits.  
  49.                           -- All threads are suspended unless 'go' is specified  
  50. trace [go] method exit | exits [thread]  
  51.                           -- trace the current method's exit, or all methods' exits  
  52.                           -- All threads are suspended unless 'go' is specified  
  53. untrace [methods]         -- stop tracing method entrys and/or exits  
  54. step                      -- execute current line  
  55. step up                   -- execute until the current method returns to its caller  
  56. stepi                     -- execute current instruction  
  57. next                      -- step one line (step OVER calls)  
  58. cont                      -- continue execution from breakpoint  
  59.   
  60. list [line number|method] -- print source code  
  61. use (or sourcepath) [source file path]  
  62.                           -- display or change the source path  
  63. exclude [<class pattern>, ... | "none"]  
  64.                           -- do not report step or method events for specified classes  
  65. classpath                 -- print classpath info from target VM  
  66.   
  67. monitor <command>         -- execute command each time the program stops  
  68. monitor                   -- list monitors  
  69. unmonitor <monitor#>      -- delete a monitor  
  70. read <filename>           -- read and execute a command file  
  71.   
  72. lock <expr>               -- print lock info for an object  
  73. threadlocks [thread id]   -- print lock info for a thread  
  74.   
  75. pop                       -- pop the stack through and including the current frame  
  76. reenter                   -- same as pop, but current frame is reentered  
  77. redefine <class id> <class file name>  
  78.                           -- redefine the code for a class  
  79.   
  80. disablegc <expr>          -- prevent garbage collection of an object  
  81. enablegc <expr>           -- permit garbage collection of an object  
  82.   
  83. !!                        -- repeat last command  
  84. <n> <command>             -- repeat command n times  
  85. # <command>               -- discard (no-op)  
  86. help (or ?)               -- list commands  
  87. version                   -- print version information  
  88. exit (or quit)            -- exit debugger  
  89.   
  90. <class id>: a full class name with package qualifiers  
  91. <class pattern>: a class name with a leading or trailing wildcard ('*')  
  92. <thread id>: thread number as reported in the 'threads' command  
  93. <expr>: a Java(tm) Programming Language expression.  
  94. Most common syntax is supported.  
  95.   
  96. Startup commands can be placed in either "jdb.ini" or ".jdbrc"  
  97. in user.home or user.dir  
  98. main[1]  

 例如输入:step,程序运行到下一步。

抱歉!评论已关闭.