📄 ch34.htm
字号:
whose default value is the folder that contains Java's classes.Java will also look in the active folder (the one you're in whenyou type the <TT>javac</TT> command line). However, you can changethe setting of CLASSPATH temporarily for the current compilationby using the <TT>-classpath</TT> option, like this:<BLOCKQUOTE><PRE>javac -classpath path FileName.java</PRE></BLOCKQUOTE><P>In the above line, <TT>path</TT> is the path you want to include,each separated by a semicolon. For example, assuming that youinstalled Java in a folder called C:\JAVA and that your own classesare in the C:\CLASSES folder, the following line compiles yourprogram using the same settings the compiler would use by default:<BLOCKQUOTE><PRE>javac -classpath c:\java\lib\classes.zip;c:\classes FileName.java</PRE></BLOCKQUOTE><P>Notice that Java's classes are in a file called CLASSES.ZIP. Youmust include this file name in the path in order for the compilerto find the classes it needs to successfully compile your applet.<H3><A NAME="SpecifyingtheTargetDirectory">Specifying the Target Directory</A></H3><P>When you run javac by typing the <TT>javac Applet.java</TT> commandline, the compiler reads the source-code file (or files), convertsit to byte-code form, and stores the resultant .CLASS file inthe directory from which the compiler was run. You can controlthis target directory by specifying the <TT>-d</TT> command option,like this:<BLOCKQUOTE><PRE>-d directory</PRE></BLOCKQUOTE><P>In this command, <TT>directory</TT> is the directory in whichyou want the output files (.CLASS files) stored.<H3><A NAME="ExampleSettingtheTargetDirectory">Example: Setting the Target Directory</A></H3><P>Suppose you have your Java source code files (the ones with the.java extension) in a folder called C:\CLASSES, as you have forthe applets you've created in this book. Now you want to havethe .CLASS files that are created by the Java compiler placedin a subdirectory of CLASSES called COMPILED. You'd first createthe subdirectory with the command <TT>md compiled</TT>. Then you'dissue the following command:<BLOCKQUOTE><PRE>javac -d c:\classes\compiled applet.java</PRE></BLOCKQUOTE><P>Following the <TT>javac</TT> command are the <TT>-d</TT> option,the name of the directory in which to store the output files,and the Java source-code file to compile.<P>The directory name is either a full path or a path relative toyour current directory. For example, if you're current directoryis c:\CLASSES, when you type the above command, you can shortenthe directory name, like this:<BLOCKQUOTE><PRE>javac -d compiled applet.java</PRE></BLOCKQUOTE><H3><A NAME="CreatingDebuggingTables">Creating Debugging Tables</A></H3><P>To get the most out of a debugger, your programs need to be compiledin a special way, so that debugging information is included inthe compiled byte-code files. The compiler switch that turns thisoption on is <TT>-g</TT>, and you use it like this:<BLOCKQUOTE><PRE>javac -g applet.java</PRE></BLOCKQUOTE><P>As you can see, the only thing extra here is the <TT>-g</TT> optionitself, which requires no additional arguments.<P>You may decide that it would be cool to use the <TT>-g</TT> optionall the time, so that your programs are always loaded with debugginginformation. Don't do it. This wouldn't be a good idea becauseprograms with debugging information are not only a bit largerthan programs without the debugging information, but also tendto run slower. The larger the program is, the more debugging informationthe <TT>-g</TT> option adds to the file.<H3><A NAME="ExampleAddingDebuggingTablestoanApplet">Example: Adding Debugging Tables to an Applet</A></H3><P>To test the <TT>-g</TT> option, copy the ShapeApplet.java filefrom the CHAP34 folder on this book's CD-ROM to your CLASSES directory.Then, compile the file with the following command:<BLOCKQUOTE><PRE>javac ShapeApplet.java</PRE></BLOCKQUOTE><P>Now, check the size of the ShapeApplet.class file. It should be1,334 bytes.<P>Next, compile the applet again, this time using the <TT>-g</TT>option, like this:<BLOCKQUOTE><PRE>javac -g ShapeApplet.java</PRE></BLOCKQUOTE><P>When you check the file size this time, you'll find it's 1,612bytes, over 20% larger. The extra size is caused by the additionaldebugging information the compiler has stored in the .CLASS file.<H3><A NAME="SuppressingWarnings">Suppressing Warnings</A></H3><P>Sometimes, when the Java compiler finds something questionablein your code, it issues a warning. Warnings represent the kindof errors that don't prevent a program from compiling properly,but that may generate a runtime error or just be bad programmingpractice. Because warnings are not critical to the compilationprocess, Java enables you to turn them off. You might do this,for example, when you already know about the problems that arecreating the warnings. To turn off the warnings, you use the <TT>-nowarn</TT>option, like this:<BLOCKQUOTE><PRE>javac -nowarn applet.java</PRE></BLOCKQUOTE><P>Like the <TT>-g</TT> option, <TT>-nowarn</TT> requires no additionalarguments.<P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>TIP</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>You can use more than one command-line option at a time. For example, you can both turn on debugging information and set the target directory with a command like this: javac -g -d c:\classes applet.java.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="OptimizingaProgram">Optimizing a Program</A></H3><P>When a compiler runs, it reads in source code and converts thatsource code to some other format, in Java's case, a byte-codefile. As a programmer, though, you know that there are many waysto accomplish the same task in a program. A compiler doesn't normallytake this sort of thing under consideration when it's working,though. It generates its output the same way for every source-codefile.<P>However, the javac compiler knows how to perform certain typesof optimization on your programs, but it only does so when asked.(And don't forget to say pretty please). To tell the Java compilerto optimize your program, you use the <TT>-O</TT> option, likethis:<BLOCKQUOTE><PRE>javac -O applet.java</PRE></BLOCKQUOTE><P>The <TT>-O</TT> option requires no arguments.<P>Notice that the letter after the hyphen is an uppercase O. A lowercaseo will not work. Also, be aware that compiling with the optimizingoption may make the resulting .CLASS file incompatible with someother Java tools. For this reason, optimizing should be done onlywhen compiling the program for the final time.<H3><A NAME="SwitchingOnVerboseOutput">Switching On Verbose Output</A></H3><P>When you run the Java compiler with no command-line option, thecompiler runs and performs its task without displaying any sortof information on the screen (unless the program contains errors).With a large program that takes a while to compile, you may wantto know what's going on behind your back, if for no other reasonthan to reassure yourself that everything is going okay. You canmake the compiler report to you as it works by using the <TT>-verbose</TT>option, like this:<BLOCKQUOTE><PRE>javac -verbose applet.java</PRE></BLOCKQUOTE><P>When you add this command-line option, the compiler will tellyou which files it's loading and compiling. It'll even tell youhow long each step took to complete (Figure 34.4).<P><A HREF="f34-4.gif"><B> Figure 34.4 : </B><I>When you use the -verbose option, the compiler reports to you every step of the way.</I></A><P><H2><A NAME="Summary"><FONT SIZE=5 COLOR=#Ff0000>Summary</FONT></A></H2><P>The compiler is one of the most important of the Java tools, becausewithout it you'd be unable to convert your source-code files intobyte-code files, which are the only kind of files that Java'sinterpreter understands. Ordinarily, you can run the compilersimply by typing the command <TT>javac</TT> followed by the nameof the file you want to compile. However, javac also recognizesa number of options that you can add to the command line. Theseinclude options to set directories, to optimize the program, toadd debugging information, and to display status information asthe compiler works. In the next chapter, you learn about the compiler'scounter-part, the interpreter, which reads the files created bythe compiler in order to run the program.<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#Ff0000>Review Questions</FONT></A></H2><OL><LI>Why do you need to use a compiler?<LI>What happens if you fail to include the .java extension whenspecifying your source-code file to the compiler?<LI>When you use options with the javac command, where do youplace them in the command line?<LI>Can you specify more than one command-line option at a time?<LI>How do you set a different target directory for the compiler'soutput files?<LI>What does the -g command-line option do?<LI>What does the -nowarn command-line option do?<LI>How do you get the compiler to show you what it's doing asit works?<LI>How do byte-code files enable Java to run the same programson different types of computers?</OL><H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#Ff0000>Review Exercises</FONT></A></H2><OL><LI>Compile an applet, instructing the compiler to include debugginginformation in the byte-code file.<LI>Compile an applet with the verbose setting, and study theinformation the compiler displays on the screen.<LI>Compile an applet, specifying no warnings, optimization, andan output directory of C:\CLASSES\MYCLASSES.</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 + -