📄 ch33.htm
字号:
these options from the command line.<BLOCKQUOTE><PRE>javadoc -classpath c:\classes -d c:\classes -verbose arcapplet.java</PRE></BLOCKQUOTE><H2><A NAME="UsingtheDisassembler"><FONT SIZE=5 COLOR=#Ff0000>Using the Disassembler</FONT></A></H2><P>The javap tool is Java's disassembler, which is a program thatcan take a byte-code (.CLASS) file and change it into a descriptionof the original source code. However, if you've ever seen theoutput from javap, you know that I'm using the word "description"loosely. This is because there's only so much information thata disassembler can determine from a compiled program. The bottomline is that the output of javap doesn't look anything like aJava program, and unless you really understand the internals ofthe Java language, you won't be able to make heads or tails outof a fully disassembled file.<P>Still, if you're brave, you can give javap a try, using the followingcommand line:<BLOCKQUOTE><PRE>javap classFile.class</PRE></BLOCKQUOTE><P>In the above, <TT>classFile.class</TT> is the name of the byte-codefile you want to disassemble. When you use javap without specifyingany options, the program displays the public data fields and methodsof the class-not too hard to understand. The javap disassemblersupports several command-line options that enable you to customizehow javap does its disassembly. Those options are <TT>-p</TT>,which tells javap to display private and protected fields andmethods along with the public ones; <TT>-c</TT>, which tells javapto display the byte-code instructions in the file; and <TT>-classpath</TT>,which gives the path javap uses to find class files.<H2><A NAME="UsingtheCHeaderGenerator"><FONT SIZE=5 COLOR=#Ff0000>Using the C Header Generator</FONT></A></H2><P>Another tool that you probably won't use much is javah, whichcreates header and stub files for use with native methods. What'sa native method? Well, because Java is an interpreted language,it tends to run a little slower than fully compiled languageslike C. For this reason, Java's creators decided to enable programmersto write methods in C when they need some extra speed and thencall those methods from within a Java program. The process ofusing native methods, however, is fairly complex, a process thateven your humble author hasn't dug too deeply into. Because Javais fast enough for 99% of the applets you'll want to write, you'renot likely to have to deal with native methods.<P>To put it simply, though, javah creates C source-code files thatenable the programmer to reference a Java class's data fieldsfrom source code written in C. If the previous sentence didn'tmake sense to you then, believe me, you don't have to worry aboutjavah at all. If you're interested in this stuff, however, youcan find information on creating native methods in your Java onlinedocumentation. There was also a pretty decent tutorial at <TT><A TARGET="resource window" HREF="http://java.sun.com/tutorial/index.html">http://java.sun.com/tutorial/index.html</A></TT>on the World Wide Web at the time of this writing. If it's stillthere, you should check it out.<P>To give you a quick overview, though, you run javah against theclass files created by the Java compiler. The javah command linelooks like this:<BLOCKQUOTE><PRE>javah className</PRE></BLOCKQUOTE><P>As is the case with many of Java's tools, javah can accept a numberof command-line options. When you specify these options in thecommand line, you place them before the class name. Table 33.2lists the options and their descriptions.<BR><P><CENTER><B>Table 33.2 Options for javah.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=166><I><B>Option</B></I></TD><TD WIDTH=411><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>-classpath path</TT></TD><TD WIDTH=411>Sets the path for finding classes.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>-d directory</TT></TD><TD WIDTH=411>Sets the directory where the output files will be stored.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>-o outputfile</TT></TD><TD WIDTH=411>Places all the output into the file specified by <TT>outputfile</TT>.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>-stubs</TT></TD><TD WIDTH=411>Tells javah to generate C declarations from the input file.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>-td directory</TT></TD><TD WIDTH=411>Sets the directory where temporary files will be stored.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>-verbose</TT></TD><TD WIDTH=411>Tells javah to display status information as it works.</TD></TR></TABLE></CENTER><P><H2><A NAME="UsingtheDebugger"><FONT SIZE=5 COLOR=#Ff0000>Using the Debugger</FONT></A></H2><P>As I write this, the Java debugger, jdb, is still not complete.Good documentation on how to use the debugger is nonexistent,with only a very brief description of the debugger available onlineat Sun. Still, the dubugger does run, and-who knows?-by the timeyou read this, it may be fully functional.<P>To use the debugger, you run your Java program with the debuggerrather than the interpreter. For example, if you were debuggingthis book's FaceApp application, you'd use a command line likethis to start the application with the debugger:<BLOCKQUOTE><PRE>jdb FaceApp</PRE></BLOCKQUOTE><P>At this point, the debugger is running, and it has loaded theFaceApp application into memory. However, FaceApp is not yet running.Instead, the debugger is waiting for its first command, whichyou enter at the command prompt (Figure 33.7).<P><A HREF="f33-7.gif"><B> Figure 33.7 : </B><I>After you load a program into the debugger, it waits for a command.</I></A><P><P>To see the list of commands supported by the debugger (not allof which work yet), type <TT>help</TT> at the command prompt.Another command you're likely to use a lot is <TT>stop at</TT>,which sets a breakpoint at a specific line in a class. A breakpointis a place where you want program execution to stop and wait foranother command. For example, if you've loaded the FaceApp applicationinto the debugger as described previously, you can now type thefollowing command to set a breakpoint at the first line of executablecode (the "4" is the line number):<BLOCKQUOTE><PRE>stop at FaceApp:4</PRE></BLOCKQUOTE><P>As you can see, the <TT>stop at</TT> command requires the classname and line number at which to stop, separated by a colon. Whenyou enter the above command, jdb lets you know that the breakpointwas set okay (Figure 33.8).<P><A HREF="f33-8.gif"><B> Figure 33.8 : </B><I>Setting breakpoints is something you do a lot with a debugger.</I></A><P><P>Now, that you have a breakpoint set, when you tell the programto execute, it'll run until it hits the breakpoint, at which timethe debugger suspends program execution and waits for anothercommand. To set this string of events into motion, type <TT>run</TT>at the debugger's command prompt. When you do, the debugger informsyou that it's running FaceApp, right after which it informs youthat it has hit a breakpoint. The debugger shows the locationof the breakpoint and waits for another command (Figure 33.9).<P><A HREF="f33-9.gif"><B> Figure 33.9 : </B><I>When the debugger hits a breakpoint, it stops and waits for your next command.</I></A><P><P>At this point, you'd probably want to do something called single-steppingthrough the program, which means telling the debugger to executethe next line of code and stop. In this way, you can trace througha program line-by-line in order to figure out where problems mightbe. Unfortunately, the debugger's <TT>step</TT> command is notyet functional, so you'll have to wait until the debugger's completeto try out single-stepping.<P>If you want to try something else with the debugger, type <TT>memory</TT>and press Enter. Now, the debugger tells you how much memory remains,as well as how much total memory there is in your system. If youtype <TT>threads</TT>, the debugger lists all the Java threadsin this system. Currently, there's only the main FaceApp thread,and, because you set a breakpoint, that thread is currently suspended.Typing <TT>threadgroups</TT>, on the other hand, gives you a listof all thread groups in the system. At this point, those groupswould be system, main, and FaceApp.<P>If you want to see the methods for any class that's loaded, youcan type <TT>methods</TT> followed by the class's name. To getthe FaceApp application running again, type <TT>cont</TT>. Tostop the debugger and get back to your regular operating systemprompt, you can type <TT>exit</TT> or <TT>quit</TT>.<P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>CAUTION</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>As I tested the debugger for this chapter, my system kept locking up, and I wasn't always able to get the results I expected. If you decide to experiment with the debugger, make sure you don't have anything else running. Be especially sure that you don't have documents that need to be saved, because the debug-ger's unpredictability is liable to cost you any work you've done on those documents. Hopefully, though, by the time you read this chapter, the debugger will be in full working order.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="Summary"><FONT SIZE=5 COLOR=#Ff0000>Summary</FONT></A></H2><P>The most important Java tools are javac (the compiler) and java(the interpreter). However, there are several other tools youcan use when developing applets and applications with Java. Someof these tools, like Appletviewer and javadoc, are handy utilitiesthat make writing Java programs easier. Others, like javah andjavap, are used in only special circumstances. In fact, it's likelythat you'll never need to use these tools. Because javac and javaare the most important Java tools, they are covered in their ownchapters, which immediately follow this one.<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#Ff0000>Review Questions</FONT></A></H2><OL><LI>How can you run more than one applet at a time with Appletviewer?<LI>Why will you have more luck with applets by using NetscapeNavigator 2.0 rather than HotJava?<LI>What's a doc-comment block?<LI>What command starts a program that's loaded into the debugger?<LI>How can you document a method's return value and parametersusing a doc-comment block?<LI>How do you start a debugging session with Appletviewer?<LI>How can you create hypertext links in the HTML files createdby javadoc?<LI>How do you start a debugging session with jdb?<LI>What does the javap tool do?<LI>What are native methods?<LI>How does javah help you use native methods?<LI>How do you set a breakpoint with the debugger?</OL><H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#Ff0000>Review Exercises</FONT></A></H2><OL><LI>Write an HTML document that loads and runs all the appletsfrom <A HREF="ch27.htm" >Chapter 27</A>. (You can find the applets in the CHAP27 folderof thisbook's CD-ROM.)<LI>Start Appletviewer for a debugging session.<LI>Add doc-comment blocks to ImageApplet.java from <A HREF="ch27.htm" >Chapter 27</A>.Run javadoc on the new source-code file in order to create theHTML documentation.<LI>Run the Java debugger on the ArgApp application from Chapter32. (You'll find the files you need in the CHAP32 folder of thisbook's CD-ROM.) When the debugger is loaded, set a breakpointand run the program.</OL><HR><HR WIDTH="100%"></P></CENTER><!-- reference library footer #1--></CENTER><IMG SRC="/images/rule.gif" WIDTH="460" HEIGHT="5" VSPACE="5"ALT="Ruler image"><br><FONT SIZE="-1">Contact <a href="mailto:reference@developer.com">reference@developer.com</a> with questions or comments.<br><a href="/legal/">Copyright 1998</a> <a href="http://www.earthweb.com" target="_top">EarthWeb Inc.</a>, All rights reserved.<BR>PLEASE READ THE <a href="/reference/usage.html">ACCEPTABLE USAGE STATEMENT</a>.<BR>Copyright 1998 Macmillan Computer Publishing. All rights reserved.</FONT></BLOCKQUOTE><!--outer table--><TD VALIGN="TOP"><!--right side ads --><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCAD1.REF" alt="Click here for more info"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCAD1.REF" alt="Click here for more info" height="88" width="88" border="0"></a><P><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCAD2.REF" alt="Click here for more info"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCAD2.REF" alt="Click here for more info" height="88" width="88" border="0"></a><P></td></tr></table></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -