📄 ch34.htm
字号:
<HTML><HEAD><TITLE>developer.com - Reference</TITLE><META NAME="Author" CONTENT="developer.com"><META NAME="Keywords" CONTENT="javascript, ActiveX, VRML, Push, channel,push channel, applet, directory, news, community, developer, internet,intranet, java, java applet, J++, javabeans, java products, tools, javatools, JDK, java development kit, java development environment, JIT,JavaPlan, enterprise tools, JVM, Java Virtual Machine, Java resources, API,browser, java documentation, CDF, Common Desktop Format, RDF, ResourceDescription Framework, Netcaster, Netcast, Castanet, meta-language, metadata,solaris, Solaris, SUN, CGI, Perl, Oracle, database, network, Netscape API,Apache, apache, SSL, mSQL, Netscape, Fasttrack, FastTrack,industry standard, ActiveX bridge, web, internet, site, sitedesign, microsoft, active server pages, asp, iis, nt, win95, 95, html, cgi,frontpage, business, corporate, vbscript, javascript, activex, isapi,database, access, sql, server, www, 3.0, 3.01, 3.02, 3.03, 3.04, 4.0,4.0b2, 4.01, 4.03, 4.04, connection, online, commerce, publisher,publishing, publish, webmaster, dynamic html, dhtml, windows 98, netscape,netscape software, shareware, magazines, jobs, japan, free, freeware, freestuff"><META NAME="description" CONTENT="Developer.com is the leading onlineservice for developers. It includes Gamelan, the official directory for Java, as wellas news, information, tutorials, and directories for other Internet technologiesincluding ActiveX, JavaScript, perl, VRML, Java Beans, push technologies, andother Internet and intranet technologies. Developer.com is also the home ofDeveloper Direct, the Professional Developer's Store, where developers canpurchase and download thousands of Web, authoring and other developmentproducts at some of today's lowest prices."><META NAME="distribution" CONTENT="global"><META NAME="resource-type" CONTENT="document"><META NAME="language" CONTENT="en"></HEAD><BODY BACKGROUND="/images/curve_r-wide.gif" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#DD0000" VLINK="#999999" ALINK="#FF0000"><TABLE><TR><TD VALIGN=TOP WIDTH=120><A HREF="/" ><IMG SRC="/images/masthead_r.gif" WIDTH="100" HEIGHT="78" BORDER="0"ALT="Developer.com Logo"></a></TD><TD><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCBAN.REF/DT=null"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCBAN.REF/DT=null" HEIGHT="60" WIDTH="468" ALT="Click here to support our advertisers"><BR><CENTER>Click here to support our advertisers</CENTER></a></TD></TR></TABLE><!--outer table--><TABLE><!--outer table--><TR><!--outer table--><TD VALIGN="TOP"><!--navbar info --><table><tr><td><A HREF="http://www.developerdirect.com/" ><IMG SRC="/images/button_s.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>SHOPPING</b></font></a></td></tr><tr><td><A HREF="/community/jobs/c_jobs.html"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>JOB BANK</B></font></a></td></tr><tr><td><A HREF="http://www.classifieds2000.com/cgi-cls/display.exe?Developer+computer+searchcomputercategories"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>CLASSIFIEDS</B></font></a></td></tr><tr><td><A HREF="/directories/directories.html"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>DIRECTORIES</B></font></a></td></tr><tr><td><A HREF="/reference/"><IMG SRC="/images/button_r.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>REFERENCE</B></font></a><br><a href="/reference/r_library.html"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000">Online Library</a></font></td></tr><tr><td><A HREF="/classroom/"><IMG SRC="/images/button_r.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>LEARNING CENTER</B></font></a></td></tr><tr><td><A HREF="/journal/"><IMG SRC="/images/button_n.gif" width="15" height="11" BORDER="0"><B><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000">JOURNAL</font></B></a></td></tr><tr><td><A HREF="/news/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>NEWS CENTRAL</b></font></a></td></tr><tr><td><A HREF="/downloads/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>DOWNLOADS</b></font></a></td></tr><tr><td><A HREF="/community/" ><IMG SRC="/images/button_c.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>COMMUNITY</B></font></a></td></tr><tr><td><A HREF="/calendar/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>CALENDAR</b></font></a></td></tr><tr><td><A HREF="/about/"><IMG SRC="/images/button_a.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>ABOUT US</b></a></font></td></tr></table><table><tr><td><form action="/subscribe/list.cgi" method=post ><img src="/images/rule.gif" width="100" height="2"><br><B><FONT FACE="Arial, Helvetica" SIZE="-1">Journal:</b></font><br><input name='email' value='your e-mail' size="11"><input type='hidden' name='state' value='Subscribe'></td></tr><tr><td><input type='submit' value='Subscribe'><br><font size="-1">Get the weekly email highlights from the most popular journal for developers!<br><a href="http://www.developer.com/subscribe.html">Current issue</a><img src="/images/rule.gif" width="100" height="2"><input type='hidden' name='name' value='New Subscriber'></FORM></td></tr></table><!--/navbar--><font size="-1"><A HREF="http://www.developer.com">developer.com</a><BR><A HREF="http://www.developerdirect.com">developerdirect.com</a><BR><A HREF="http://www.htmlgoodies.com">htmlgoodies.com</a><BR><A HREF="http://www.javagoodies.com">javagoodies.com</a><br><A HREF="http://www.jars.com">jars.com</a><BR><a href="http://www.intranetjournal.com">intranetjournal.com</a><br><a href="http://www.javascripts.com">javascripts.com</a><br></font><!--outer table--></TD><!--outer table--><TD VALIGN="TOP"><BLOCKQUOTE><P><img src="/images/header_r.gif" width="440" height="50"><P><MAP NAME ="header_r2.gif.map"><AREA SHAPE ="RECT" HREF ="/reference/author.html" COORDS ="361,0,440,29"><AREA SHAPE ="RECT" HREF ="/reference/basic.html" COORDS ="254,0,360,29"><AREA SHAPE ="RECT" HREF ="/reference/help.html" COORDS ="183,0,251,29"><AREA SHAPE ="RECT" HREF ="/reference/cool.html" COORDS ="100,0,181,29"><AREA SHAPE ="RECT" HREF ="/reference/mybook.html" COORDS ="0,0,100,29"></MAP><MAP NAME ="header_r1.gif.map"><AREA SHAPE ="RECT" HREF ="/reference/personalize.html" COORDS ="339,0,439,29"></MAP><p><font face="Arial,Helvetica" size="-1" color="#006666"><A HREF="/reference/r_library.html"><B>All Categories</A> :</B><b><A HREF="/reference/r_java.html">Java</A></b></font><p><META><H1><FONT SIZE=6 COLOR=#Ff0000>Chapter 34</FONT></H1><H1><FONT SIZE=6 COLOR=#Ff0000>Using the Compiler</FONT></H1><HR><P><CENTER><B><FONT SIZE=5><A NAME="CONTENTS">CONTENTS</A></FONT></B></CENTER><UL><LI><A HREF="#WhattheCompilerDoes">What the Compiler Does</A><LI><A HREF="#RunningtheCompiler">Running the Compiler</A><UL><LI><A HREF="#SettingtheClassPath">Setting the Class Path</A><LI><A HREF="#SpecifyingtheTargetDirectory">Specifying the Target Directory</A><LI><A HREF="#ExampleSettingtheTargetDirectory">Example: Setting the Target Directory</A><LI><A HREF="#CreatingDebuggingTables">Creating Debugging Tables</A><LI><A HREF="#ExampleAddingDebuggingTablestoanApplet">Example: Adding Debugging Tables to an Applet</A><LI><A HREF="#SuppressingWarnings">Suppressing Warnings</A><LI><A HREF="#OptimizingaProgram">Optimizing a Program</A><LI><A HREF="#SwitchingOnVerboseOutput">Switching On Verbose Output</A></UL><LI><A HREF="#Summary">Summary</A><LI><A HREF="#ReviewQuestions">Review Questions</A><LI><A HREF="#ReviewExercises">Review Exercises</A></UL><HR><P>The one Java tool that you have to use constantly is the compiler,javac. This is because your Java source-code files are meaningfulonly to human readers. Your computer cannot understand them atall. The compiler's job is to take your program and convert itfrom human-readable form to machine-readable form. You've beenusing the Java compiler all throughout this book, but in thischapter, you'll learn some new tricks and tips that'll help youget the best results from the compiler.<H2><A NAME="WhattheCompilerDoes"><FONT SIZE=5 COLOR=#Ff0000>What the Compiler Does</FONT></A></H2><P>In languages such a C, a compiler converts source code (the stuffyou write) to machine language, which your computer can executedirectly. Because every type of computer uses a different formof machine code, programs that must run on different types ofmachines must be compiled specifically for each machine. For example,a C program compiled on a Windows machine cannot be run on a Macintosh,and vice versa. To run that Windows program on the Macintosh,the program would first have to be compiled by a Macintosh compiler(assuming that the source code was portable, meaning that thecode contained no machine -specific instructions).<P>With Java, however, the compiler converts your source code intobyte-code files, which are the same format on every machine. Thismeans that a Java program compiled on a Windows machine will runequally as well on a Solaris or Macintosh machine. This feat ofdigital magic is possible because the Java byte-code files arenot read directly by the machine. Instead, Java's interpreterreads the byte-code files and translates them into machine codefor the specific machine on which the Java program is running.<P>The interpreter does this translation as the applet or applicationis running, meaning that the whole process is transparent to theuser. The interpreter, of course, must be specially written andcompiled for each type of machine that wants to run Java programs.Figure 34.1 illustrates this concept. From the user's point ofview, though, this simply means that he must have Java installedon his system. The user doesn't need to know anything about theinterpreter; it works automatically when the user views an appletin his Web browser.<P><A HREF="f34-1.gif"><B> Figure 34.1 : </B><I>Because Java programs are interpreted, they can be run on any machine that has a Java interpreter.</I></A><P><H2><A NAME="RunningtheCompiler"><FONT SIZE=5 COLOR=#Ff0000>Running the Compiler</FONT></A></H2><P>As you already know, you can run the Java compiler with a commandline that consists of the compiler's name followed by the nameof the file to compile, like this:<BLOCKQUOTE><PRE>javac Applet.java</PRE></BLOCKQUOTE><P>There are a couple of important things to remember about thiscommand. First, the Java compiler is case-sensitive when it comesto comparing source-code file names with the names of the classcontained in the file. For example, if your source-code file isnamed shapeapplet.java and the class it defines is <TT>ShapeApplet</TT>,the Java compiler will complain and not compile the file (Figure34.2).<P><A HREF="f34-2.gif"><B> Figure 34.2 : </B><I>The source-code file name must match the name of the public class defined in the file.</I></A><P><P>Second, the compiler requires that you include the source-codefile's extension, which should always be .java. Notice that theextension is all lowercase. If you fail to include the extensionwhen running the compiler, or if you fail to save your source-codefile with the proper extension, you will get an "invalidargument" error (Figure 34.3). I'm not sure why Java's creatorsinsist upon the file extension being present when you type thejavac command line. Most compilers assume the proper file extensionwhen the file name is typed without the extension. Not so withJava. Strange.<P><A HREF="f34-3.gif"><B> Figure 34.3 : </B><I>A source code name without the proper .java extension is an invalid argument.</I></A><P><P>Like many of the tools included with java, the compiler recognizessome command options that you can add to the command line. Table34.1 lists these command options and their meanings.<BR><P><CENTER><B>Table 34.1 Command Options for Javac.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=166><I><B>Option</B></I></TD><TD WIDTH=424><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>-classpath path</TT></TD><TD WIDTH=424>Determines the path in which the compiler looks for classes.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>-d directory</TT></TD><TD WIDTH=424>Determines the directory in which javac stores the output files.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>-g</TT></TD><TD WIDTH=424>Tells javac to create debugging information, which is used by debugging tools.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>-nowarn</TT></TD><TD WIDTH=424>Tells javac not to display warnings as it compiles a file.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>-O</TT></TD><TD WIDTH=424>Tells java to optimize the compiled program.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>-verbose</TT></TD><TD WIDTH=424>Tells javac to display status information as it works.</TD></TR></TABLE></CENTER><P><P>To use any of these options, place the options between the <TT>javac</TT>command and the source file name, like this:<BLOCKQUOTE><PRE>javac options filename.java</PRE></BLOCKQUOTE><P>In the following sections, you'll look at each of the compileroptions in detail. In some cases, you'll even get some hands-onexperience.<H3><A NAME="SettingtheClassPath">Setting the Class Path</A></H3><P>In order to compile an applet, the compiler usually needs to drawupon other already compiled files. These files might be filesthat you've created for custom classes or they may be the classfiles that make up the class hierarchy of the class you're compiling.For example, when you derive your applet from Java's <TT>Applet</TT>class, the compiler needs to know about the <TT>Applet</TT> classin order to fully compile your applet. Moreover, because Java's<TT>Applet</TT> class itself is a subclass of yet other Java classes,the compiler needs to bring in many different class files. Obviously,before the compiler can access these class files, it has to knowwhere they are.<P>Normally, when you compile a program, the compiler finds classesusing the current setting of your system's CLASSPATH variable,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -