`
liugang594
  • 浏览: 978121 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

 
阅读更多

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

 

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

 

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

 

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

 

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

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上打印一条类似如下消息:

Listening for transport dt_socket at address: 3556

 

表明程序启动成功。

 

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

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

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

Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
Initializing jdb ...
>
VM Started: No frames on the current call stack

main[1]

 

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

 

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

main[1] help
** command list **
connectors                -- list available connectors and transports in this VM

run [class [args]]        -- start execution of application's main class

threads [threadgroup]     -- list threads
thread <thread id>        -- set default thread
suspend [thread id(s)]    -- suspend threads (default: all)
resume [thread id(s)]     -- resume threads (default: all)
where [<thread id> | all] -- dump a thread's stack
wherei [<thread id> | all]-- dump a thread's stack, with pc info
up [n frames]             -- move up a thread's stack
down [n frames]           -- move down a thread's stack
kill <thread id> <expr>   -- kill a thread with the given exception object
interrupt <thread id>     -- interrupt a thread

print <expr>              -- print value of expression
dump <expr>               -- print all object information
eval <expr>               -- evaluate expression (same as print)
set <lvalue> = <expr>     -- assign new value to field/variable/array element
locals                    -- print all local variables in current stack frame

classes                   -- list currently known classes
class <class id>          -- show details of named class
methods <class id>        -- list a class's methods
fields <class id>         -- list a class's fields

threadgroups              -- list threadgroups
threadgroup <name>        -- set current threadgroup

stop in <class id>.<method>[(argument_type,...)]
                          -- set a breakpoint in a method
stop at <class id>:<line> -- set a breakpoint at a line
clear <class id>.<method>[(argument_type,...)]
                          -- clear a breakpoint in a method
clear <class id>:<line>   -- clear a breakpoint at a line
clear                     -- list breakpoints
catch [uncaught|caught|all] <class id>|<class pattern>
                          -- break when specified exception occurs
ignore [uncaught|caught|all] <class id>|<class pattern>
                          -- cancel 'catch' for the specified exception
watch [access|all] <class id>.<field name>
                          -- watch access/modifications to a field
unwatch [access|all] <class id>.<field name>
                          -- discontinue watching access/modifications to a field
trace [go] methods [thread]
                          -- trace method entries and exits.
                          -- All threads are suspended unless 'go' is specified
trace [go] method exit | exits [thread]
                          -- trace the current method's exit, or all methods' exits
                          -- All threads are suspended unless 'go' is specified
untrace [methods]         -- stop tracing method entrys and/or exits
step                      -- execute current line
step up                   -- execute until the current method returns to its caller
stepi                     -- execute current instruction
next                      -- step one line (step OVER calls)
cont                      -- continue execution from breakpoint

list [line number|method] -- print source code
use (or sourcepath) [source file path]
                          -- display or change the source path
exclude [<class pattern>, ... | "none"]
                          -- do not report step or method events for specified classes
classpath                 -- print classpath info from target VM

monitor <command>         -- execute command each time the program stops
monitor                   -- list monitors
unmonitor <monitor#>      -- delete a monitor
read <filename>           -- read and execute a command file

lock <expr>               -- print lock info for an object
threadlocks [thread id]   -- print lock info for a thread

pop                       -- pop the stack through and including the current frame
reenter                   -- same as pop, but current frame is reentered
redefine <class id> <class file name>
                          -- redefine the code for a class

disablegc <expr>          -- prevent garbage collection of an object
enablegc <expr>           -- permit garbage collection of an object

!!                        -- repeat last command
<n> <command>             -- repeat command n times
# <command>               -- discard (no-op)
help (or ?)               -- list commands
version                   -- print version information
exit (or quit)            -- exit debugger

<class id>: a full class name with package qualifiers
<class pattern>: a class name with a leading or trailing wildcard ('*')
<thread id>: thread number as reported in the 'threads' command
<expr>: a Java(tm) Programming Language expression.
Most common syntax is supported.

Startup commands can be placed in either "jdb.ini" or ".jdbrc"
in user.home or user.dir
main[1]

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

 

 

分享到:
评论

相关推荐

    Mac 创建虚拟串口 Java 程序让 RXTXcomm 使用

    Mac 在创建虚拟串口上没有 Windows 方便,windows 有 VSPD 用起来非常方便,但是 Mac 就没有那么方便了,本 java 程序基于 socat 和 jdk8,可以自动创建一对虚拟串口,以供 RXTX comm 调试使用

    java连接数据库课程设计(1).doc

    *******大 学 学 生 实 验 报 告 实验课程名称 J2EE及JAVA程序设计 开课实验室 DS1501 学 院 软件学院 年级 2012 专业班 2班 学 生 姓 名 学 号 开 课 时 间 2013 至 2014 学年第 2 学期 "总 成 绩 " " "教师签名 " ...

    java程序是怎么操作数据库的,可以以常用据库为例,求详细解答,最好能举例。

    最后需要说的是笔者的调试上面程序的环境:WIN2000 , JDK1.3,MS SQLSERVER编辑软件:EDITPLUS 2.01a(这最后的东西可不是废话,虽然早就了一些专业的JAVA开发工具,但是笔者建议JAVA初学者使用文本软件开发JAVA程序)...

    jdivisitor:Java库,用于使用Java调试接口(JDI)和访问者设计模式来构建自定义调试器应用程序

    执照GNU GPL v2.0用法以下步骤可用于创建自定义调试器应用程序: 实现一个EventRequestor类,该类请求将由调试器处理的特定JDI事件实现一个EventVisitor类,该类描述如何处理在步骤1中启用的JDI事件使用...

    JAVA程序设计教程

    1.3.3 Java程序的运行 ............................................................................................10 1.3.4 Java程序的调试 .............................................................

    疯狂java光盘内容

    运行Java程序,还应该在PATH环境变量中增加%JAVA_HOME%/bin。其中JAVA_HOME代表 JDK(不是JRE)的安装路径。 如何安装上面工具,请参考本书的第1章。 (2) 安装MySQL 5.5或更高版本,安装MySQL时选择GBK编码方式...

    JAVA程序开发大全---上半部分

    本书内容精练、重点突出、实例丰富,是Java程序开发人员必备的参考书,同时也可作为高等院校计算机及相关专业的教材。 图书目录 第1章 初识MyEclipse 1 1.1 MyEclipse简介 1 1.2 MyEclipse的安装 1 1.2.1 JDK的...

    Java系统部署文档-windows-V1.0.1.doc

    可以通过控制台输出、日志文件等方式来监控和调试应用程序的运行状态。 注意事项: 在部署Java应用程序时,需要注意版本和兼容性,防止不同版本的Java环境、类库或框架之间产生冲突和错误。 在配置服务器时,需要...

    Java连接mysql

    本示例已调试通过,在Java程序里连接Mysql数据库,通过更改配置文件,可用不同角色连不同数据库,本示例是连的mysql,同样,通过更改配置文件,可连Sql Server、Oracle,内附详细说明,机器环境变量的设置、mysql的角色的...

    疯狂Java讲义第二版配套光盘源码

    运行Java程序,还应该在PATH环境变量中增加%JAVA_HOME%/bin。其中JAVA_HOME代表 JDK(不是JRE)的安装路径。 如何安装上面工具,请参考本书的第1章。 (2) 安装MySQL 5.5或更高版本,安装MySQL时选择GBK编码方式...

    Java课程设计报告书

    一、课程设计目的: 1. 学会Java程序开发的环境搭建与配置,并在实际运用中学习和掌握Java程序开发的全过程。 2. 进一步熟悉掌握Java程序设计语言的基础内容,如用户图形界面设计、Java多线程编程、Applet程序...

    java图书管理系统毕业设计(源代码+论文)-jsp-java源程序代码.rar

    在Visual J++中,用户可以创建、修改、编译、运行、调试和封装应用程序,所有这些操作都在单一的环境中进行。 Visual j++6.0介绍Java的Windows基础类(WFC),这个新的应用程序框架访问Microsoft Windows API,允许...

    java连接数据库课程设计.doc

    ****大 学 学 生 实 验 报 告 实验课程名称 J2EE及JAVA程序设计 开课实验室 DS1501 学 院 软件学院 年级 2012 专业班 1 班 学 生 姓 名 学 号 开 课 时 间 2013 至 2014 学年第 2 学期 "总 成 绩 " " "教师签名 " " ...

    程序行为记录与跟踪

    程序在运行期间所进行的操作被称为“程序行为”(Action),一般泛指程序进行的相对表现较明显的操作,例如创建读写文件、访问注册表、连接网络等,而在这些操作之外做的程序内部运算、判断、逻辑等操作并不是我们...

    java程序设计实习报告---学生信息管理系统的设计与开发.doc

    5 六、调试过程 5 七、实习小结 5 八、参考资料 6 一、实习题目 学生信息管理系统的设计与开发 二、实习目的 1、社会需求 一直以来学生的成绩管理是学校工作中的一项重要内容,随着办学规模的扩大和招生 人数的增加...

    Java2核心技术.part5

    第1章Java程序设计概述 1.1 Java程序设计平台 1.2 Java“白皮书”的关键术语 1.2.1简单性 1.2.2面向对象 1.2. 3分布式 1. 2.4健壮性 1. 2.5安仝性 1. 2.6体系结构中立 1.2.7可移植性 1.2.8...

    JVM-Command:使用Java代理覆盖标准JRE类,以使我们能够对Java应用程序进行密集调试

    大量调试(破解)在JVM内部运行的Java应用程序。 快速将付费脚本的ClassFiles转储为runescape机器人。 (rofl) 找出应用程序连接到的套接字和URL。 谁他妈的知道,谁玩弄,并采取控制! 如何使用: 下载包含...

Global site tag (gtag.js) - Google Analytics