`
liugang594
  • 浏览: 981623 次
  • 性别: 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,程序运行到下一步。

 

 

分享到:
评论

相关推荐

    JAVA Udp调试工具 Java版本

    Java UDP调试工具是一种用于测试和调试UDP(User Datagram Protocol)网络通信的应用程序。UDP是一种无连接的传输协议,常用于需要高效传输且对数据完整性要求不高的场景,如在线视频流、实时游戏等。该工具通常包含...

    JCreator的java应用程序调试设置

    **Java应用程序调试在JCreator中的配置设置** JCreator是一款流行的Java集成开发环境(IDE),它为程序员提供了便捷的代码编辑、编译、运行以及调试功能。对于初学者和专业开发者而言,理解如何在JCreator中设置...

    JAVA程序与PLC通信——学习使用

    本篇将详细探讨"JAVA程序与PLC通信——学习使用"这一主题,包括如何使用Java实现与西门子PLC的串口通信,以及如何收发数据。 首先,我们要理解Java语言的优势。Java作为一种跨平台的编程语言,其强大的网络支持和...

    JAVA程序与PLC之间的通信

    在IT行业中,Java程序与PLC(可编程逻辑控制器)之间的通信是一个重要的技术领域,尤其在工业自动化系统中。这个主题涉及到如何利用编程语言与硬件设备进行数据交换,以实现远程控制、监控或数据采集等功能。以下是...

    java 远程调试 ppt, 内有demo,简单实用

    这通常包括确保JVM已配置为接受调试连接,服务器上的防火墙设置允许调试端口的入站连接,以及在本地开发环境中设置相应的远程调试配置。有时,你可能需要使用SSH隧道来安全地连接到远程服务器的调试端口。 最后,...

    java的经典安装程序

    "jdk-6u17-windows-i586.exe"文件是Java JDK的可执行安装程序,它包含了开发和运行Java应用程序所需的所有组件,包括Java虚拟机(JVM)、编译器(javac)、调试器(jdb)和其他工具。JDK 6是Java的一个重要版本,提供了...

    初学者在调试程序遇到的错误小结

    "初学者在调试程序遇到的错误小结" 本文总结了初学者在使用 Eclipse 和 Java 以及 Spring 框架时遇到的常见错误,并提供了相应的解决方案。 一、数据库方面的错误 * 出现 DataAccessException 这样的错误,一般是...

    大数据必学Java基础(三十二):IDEA中的断点调试

    IDEA中的断点调试是Java开发中非常重要的一部分,它可以帮助开发者更好地了解代码的执行过程,Debug程序中的错误,提高开发效率。下面将详细介绍IDEA中的断点调试知识点。 一、常用断点调试快捷键 在IDEA中,Debug...

    java 开发第一个Java程序

    JDK包含了编译器(javac)和解释器(java),以及其他必要的工具,用于创建、编译和运行Java程序。 标题中的"Example1.java"是你的第一个Java源代码文件。源代码文件通常以.java为扩展名,它包含了用Java语言编写的...

    《Java程序设计之网络编程》

    《Java程序设计之网络编程》是一本专注于Java网络编程的教材,它涵盖了网络通信的基础理论以及Java语言在实现网络应用中的各种技术。该资源包括课件和源码,旨在帮助学习者通过实践来深入理解Java网络编程的核心概念...

    JAVA程序设计教程(包含上机调试源码)

    Java程序设计教程是初学者和进阶者深入理解并掌握Java编程语言的重要资源。这个教程不仅提供了详尽的理论知识,还包含了丰富的实践环节,通过上机调试源码,可以帮助学习者直观地理解代码的运行过程,提升编程技能。...

    Java程序设计实用教程源代码与习题

    《Java程序设计实用教程》是叶核亚编著的一本针对初学者和进阶者学习Java编程语言的经典教材。本书深入浅出地介绍了Java的基本概念、语法和编程技巧,旨在帮助读者掌握Java程序设计的核心技能。书中源代码和课后习题...

    微信小程序连接java后端

    微信小程序连接Java后端主要涉及的是前后端交互的技术实现,这一过程涵盖了多个技术知识点,包括微信小程序的开发、Java后端API设计与实现、数据传输协议以及认证授权机制等。以下将详细介绍这些方面: 1. **微信小...

    java运行环境平台

    在实际开发中,JRE与Java Development Kit(JDK)的区别在于,JDK是用于开发和调试Java程序的完整工具集,它包含了JRE以及编译器(javac)、jar打包工具、文档生成工具等开发工具。而JRE则专注于提供运行Java程序所...

    java程序设计课程代码

    Java程序设计是一门深入探讨如何使用Java编程语言进行软件开发的学科。这门课程的核心是学习如何编写高效、可读性强的代码,并掌握各种编程概念和技术。以下是对标题和描述中涉及的知识点的详细说明: 1. **Java...

    java程序设计实用教程第三版

    《Java程序设计实用教程第三版》是一本专为初学者和有一定基础的程序员设计的Java学习教材。本书全面覆盖了Java编程的基础知识,包括语法、类与对象、接口与继承、异常处理、多线程、网络编程、数据库操作等多个方面...

    Java源码获取程序运行环境的信息

    在Java编程中,获取程序运行环境的信息是一项基本且重要的任务,这有助于开发者了解程序的运行状态,调试问题,以及优化代码。以下是一些关键的知识点,涵盖了如何通过Java源码来获取程序运行环境的信息。 1. **...

    Eclipse远程调试Java代码

    远程调试允许开发者在本地IDE上对运行在另一台机器上的应用程序进行调试。这通常通过JVM的远程调试接口实现,即使用标准的Java Debug Wire Protocol (JDWP)。JDWP允许IDE与远程JVM建立连接,进而控制程序执行,设置...

    JAVA程序设计与问题解决(电子书)

    《JAVA程序设计与问题解决》是一本全面介绍Java编程语言的教材,分为基础篇和高级篇,由美国知名计算机科学家Walter Savitch撰写,并有中文翻译版本。这本书旨在帮助读者从零开始学习Java编程,逐步掌握解决问题的...

    java连接Mysql5.0驱动与实例(调试成功)

    Java连接MySQL 5.0是Java开发者...这个压缩包提供的实例将展示如何实现上述步骤,帮助初学者快速上手Java与MySQL的连接。通过实际运行和调试这些代码,你可以更好地理解JDBC的工作原理,并学会在自己的项目中使用它们。

Global site tag (gtag.js) - Google Analytics