📄 running.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Writing Advanced Applications, Chapter 7: Running Tests & Analyzing</TITLE>
<META NAME="AUTHOR" CONTENT="Monica Pawlan and Calvin Austin">
<META NAME="KEYWORDS" CONTENT="programming, advanced, Java 2">
<META NAME="OWNER" CONTENT="Editorial/JDC">
<META NAME="revision" CONTENT="@(#)running.src 1.12 09/21/99 JDC">
</HEAD>
<!-- Start Body Insert-->
<BODY BGCOLOR="#ffffff">
<!-- End Body Insert-->
<!-- Start PageTop Insert -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR ALIGN="CENTER" VALIGN="TOP">
<TD WIDTH="157" ALIGN="LEFT">
<IMG SRC="/images/pixel.gif" HEIGHT="40" WIDTH="40" ALT="">
<A HREF="http://java.sun.com/index.html"><IMG SRC="/images/javalogo52x88.gif" WIDTH="52" HEIGHT="88" ALT="Java Technology Home Page" BORDER="0"></A>
<BR>
<IMG SRC="/images/pixel.gif" WIDTH="157" HEIGHT="1" ALT=""></TD>
<TD>
<FORM NAME="seek1" METHOD="GET" ACTION="http://search.java.sun.com/query.html">
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
<TR>
<TD ALIGN="RIGHT">
<IMG SRC="/images/stripelt.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>
<TD WIDTH="100%">
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
<TR>
<TD BGCOLOR="#CC9966" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
</TABLE>
</TD>
<TD ALIGN="LEFT">
<IMG SRC="/images/stripert.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>
<TD>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD VALIGN="CENTER">
<A HREF="http://java.sun.com/a-z/index.html"><IMG SRC="/images/azindex.gif" BORDER="0" WIDTH="72" HEIGHT="11" ALT="A-Z Index"></A></TD>
<TD VALIGN="CENTER">
<FONT FACE="Helvetica" SIZE="1">
<INPUT TYPE="text" SIZE="15" MAXLENGTH="128" NAME=qt></FONT></TD>
<TD VALIGN="CENTER">
<INPUT TYPE="image" SRC="/images/search.button.gif"
value="search" BORDER="0" WIDTH="55" HEIGHT="14" ALT="Search"></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</FORM>
<P>
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="0" CELLSPACING="0">
<TR VALIGN="TOP">
<TD WIDTH="100%"><IMG SRC="/images/chiclet.row.gif" WIDTH="55"
HEIGHT="18" ALT=""></TD>
<TD ROWSPAN="4" ALIGN="RIGHT" WIDTH="152">
<A HREF="/developer/index.html"><IMG SRC="/images/developer.connection.header.gif" BORDER="0" HEIGHT="42" WIDTH="319" ALT="Java Developer Connection(SM)"></A></TD>
</TR>
<TR VALIGN="TOP">
<TD BGCOLOR="#FFFFFF" HEIGHT="1" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR VALIGN="TOP">
<TD BGCOLOR="#CC9966" HEIGHT="1" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR VALIGN="TOP">
<TD><A HREF="/developer/onlineTraining/"><IMG SRC="/images/online-training.gif" ALT="Online Training" BORDER=0></A></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<!-- End PageTop Insert -->
<!-- Start NavBar Insert -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="3" BGCOLOR="#FFFFFF" WIDTH="157" ALIGN="LEFT">
<!-- tab categories -->
<TR>
<TD><A HREF="http://java.sun.com/products/"><IMG SRC="/images/side.tab.products.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Downloads, APIs, Documentation"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/index.html"><IMG SRC="/images/side.tab.developer.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Java Developer Connection"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/infodocs/index.shtml"><IMG SRC="/images/side.tab.docs.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Tutorials, Tech Articles, Training"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/support/index.html"><IMG SRC="/images/side.tab.support.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Online Support"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/community/index.html"><IMG SRC="/images/side.tab.community.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Community Discussion"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/industry/"><IMG SRC="/images/side.tab.news.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="News & Events from Everywhere"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/solutions"><IMG SRC="/images/side.tab.solutions.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Products from Everywhere"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/casestudies"><IMG SRC="/images/side.tab.case.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="How Java Technology is Used Worldwide"></A></TD>
</TR>
<TR><TD> </TD></TR>
<!-- End NavBar Insert -->
<!-- START SUB-NAV -->
<TR>
<TD><!-- INSERT SUB-NAV INFO -->
</TD>
</TR>
<!-- END SUB-NAV -->
</TABLE>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD>
<!-- Template Version 2.0 -->
<!-- ================== -->
<!-- Start Main Content -->
<!-- ================== -->
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD VALIGN="TOP">
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A NAME="top"></A>
<DIV ALIGN="RIGHT">
<FONT SIZE="-1"><A HREF="/developer/onlineTraining/index.html">Training Index</A></FONT>
<H2>Writing Advanced Applications<BR>
<FONT SIZE="3">Chapter 7 Continued: Running Tests and Analyzing</FONT></H2>
<FONT SIZE="-1">[<A HREF="collect.html"><<BACK</A>] [<A HREF="index.html#contents">CONTENTS</A>] [<A HREF="servlet.html">NEXT>></A>]</FONT></DIV>
<P>
If you are still having problems even after you have ruled out installation
and environment problems and included debugging code, it is time to
use tools to test and analyze your program.
<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#seat">Getting Behind the Seat with jdb</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#simple">Simple jdb Test Drive</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#remote">Remote Debugging</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#auto">Using Auto-Pilot</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#session">Creating a Session Log</A></FONT>
</UL>
<HR>
<A NAME="seat"></A>
<H3>Getting Behind the Seat with jdb</H3>
Although there are some very good integrated development environment (IDE)
tools on the market, the Java<FONT SIZE="-2"><SUP>TM</SUP></FONT> debugger tool, <CODE>jdb</CODE> and
its successors have an important role to play in testing and debugging
programs. Some advantages of <CODE>jdb</CODE> over IDE tools are it is
free, it is platform independent (some IDE tools are not),
and it runs as a separate process to the program it is debugging. The
benefit to <CODE>jdb</CODE> running as a separate process is
you can attach a debug session to a running program.
<P>
The downsides to using <CODE>jdb</CODE> are it has only a command-line
interface, and it relies on the same code you are trying to debug. This
means if there is a bug in the Java VM, <CODE>jdb</CODE> could break
attempting to diagnose that same bug!
<P>
The new <CODE>JBUG</CODE> architecture was created to solve these problems
in <CODE>jdb</CODE>. <CODE>JBUG</CODE>, amongst other things,
provides a debugger helper API in the Java VM called the Java VM Debug
Interface (JVMDI). This helper communicates with the debugging
front end using the Java Debug Wire Protocol (JDWP). The debugging front
end uses the remote Java Debug Interface (JDI) to send and receive
commands over the Java Debug Wire Protocol.
JBug is available for Java 2 platforms, and has a <CODE>jdb</CODE> style front
end that you will learn more about later.
<A NAME="simple"></A>
<H3>Simple jdb Test Drive</H3>
Back to the classic <CODE>jdb</CODE> tool. Here are some simple steps to analyze
a program using <CODE>jdb</CODE>. This first example debugs a program from
application startup. The <A HREF="#remote">Remote Debugging</A>
example shows how to connect to a running program.
<P>
<H4>Start the Session</H4>
<P>
To begin the debug session, compile the
<A HREF="./Code/SimpleJdbTest.java"> SimpleJdbTest.java</A>
program with full debugging information using <CODE>javac</CODE>
and the -g debug flag as shown. In this example, the
<CODE>SimpleJdbTest.java</CODE> program is an application,
but it could just as well be an applet. The procedures
for debugging applications with <CODE>jdb</CODE> are the
same for debugging applets once the debug session has started.
</FONT>
<PRE>
javac -g SimpleJdbTest.java
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
Next, start the <CODE>jdb</CODE> tool with the program class
name as a parameter:
</FONT>
<PRE>
jdb SimpleJdbTest
Initializing jdb...
0xad:class(SimpleJdbTest)
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<p>
To debug an applet in <CODE>appletviewer</CODE> use the <CODE>-debug</CODE>
parameter as in this example:
</FONT>
<PRE>
$ appletviewer -debug MyApplet.html
Initializing jdb...
0xee2f9808:class(sun.applet.AppletViewer)
>
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<H4>Setting a Breakpoint and Listing Methods</H4>
At this point, the <CODE>SimpleJdbTest</CODE> class has only been loaded;
the class constructor has not been called. To make <CODE>jdb</CODE>
stop when the program is first instantiated, put a stop, or breakpoint, at
the constructor using the <CODE>stop in</CODE> command.
When the breakpoints has been set, instruct <CODE>jdb</CODE> to run
your program using the <CODE>run</CODE> command as follows:
</FONT>
<PRE>
stop in SimpleJdbTest.<init>
Breakpoint set in SimpleJdbTest.<init>
run
run SimpleJdbTest
running ...
main[1]
Breakpoint hit: SimpleJdbTest.<init>
(SimpleJdbTest:10)
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<P>
The <CODE>jdb</CODE> tool stops at the first line in the constructor. To list
the methods that were called to get to this breakpoint, enter
the <CODE>where</CODE> command:
</FONT>
<PRE>
main[1] where
[1] SimpleJdbTest.<init> (SimpleJdbTest:10)
[2] SimpleJdbTest.main (SimpleJdbTest:29)
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<P>
The numbered method in the list is the last stack frame that
the Java VM has reached. In this case the last stack frame
is the <CODE>SimpleJdbTest</CODE> constructor that was called from
<CODE>SimpleJdbTest</CODE> main.
<P>
Whenever a new method is called, it is placed on this stack list. The Hotspot
technology achieves some of its speed gains by eliminating a new stack
frame when a new method is called. To gain a general appreciation of where
the code has stopped, enter the <CODE>list</CODE> command.
</FONT>
<PRE>
main[1] list
6 Panel p;
7 Button b;
8 int counter=0;
9
10 SimpleJdbTest() {
11 setSize(100,200);
12 setup();
13 }
14 void setup (){
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<P>
<H4>Locating the Source</H4>
<P>
If the source to the class file stopped in is not available
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -