📄 ch35.htm
字号:
</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>-mx x</TT></TD><TD WIDTH=424>Specifies the maximum amount of memory that can be allocated for the session.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>-noasyncgc</TT></TD><TD WIDTH=424>Tells Java not to use asynchronous garbage collection.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>-noverify</TT></TD><TD WIDTH=424>Tells the interpreter not to verify code.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>-oss x</TT></TD><TD WIDTH=424>Specifies the maximum stack size for Java code.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>-ss x</TT></TD><TD WIDTH=424>Specifies the maximum stack size for C code.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>-v</TT></TD><TD WIDTH=424>Specifies that the interpreter should display status information as it works.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>-verbose</TT></TD><TD WIDTH=424>Same as <TT>-v</TT>.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>-verbosegc</TT></TD><TD WIDTH=424>Specifies that the garbage collector should display status information as it works.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>-verify</TT></TD><TD WIDTH=424>Tells the interpreter to verify all Java code.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>-verifyremote</TT></TD><TD WIDTH=424>Tells the interpreter to verify code loaded by a classloader. This option is the default.</TD></TR></TABLE></CENTER><P><P>As you can see, the interpreter can accept quite a few command-lineoptions. Of these options, though, only a few are used frequently.You'll get a look at those more useful options in the sectionsthat follow.<H3><A NAME="KeepingFilesUptoDate">Keeping Files Up to Date</A></H3><P>When you're working on a new application, you'll make frequentchanges to the source code. Whenever you change the source code,you must recompile the program before you run it. Otherwise, you'llbe running an old version of the program. When you start writinglarger applications, you'll have many files for the classes thatare used in the program. As you change the contents of these files,you may lose track of which files need to be recompiled. Thisis where the interpreter's <TT>-checksource</TT> command-lineoption comes into play.<P>The <TT>-checksource</TT> option tells the interpreter to comparethe dates and times of your source-code files with the dates andtimes of the matching .CLASS files. When a source-code file isnewer than the matching .CLASS file, the interpreter automaticallyruns the compiler to bring the files up to date. You use the <TT>-checksource</TT>option like this:<BLOCKQUOTE><PRE>java -checksource appname</PRE></BLOCKQUOTE><P>Here, <TT>appname</TT> is the name of the class you want the interpreterto run.<P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>When running a standalone application, any arguments that you place after the name of the file to execute are passed to the application's <TT>main()</TT> method. For more information on handling these application arguments, please refer to <A HREF="ch32.htm" >Chapter 32</A>, "Writing Java Applications."</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="SettingtheClassPath">Setting the Class Path</A></H3><P>In order to run a standalone application, the interpreter usuallyneeds to load class files that are used by the program. Thesefiles might be files that you've created for custom classes orthey may be the class files that make up the class hierarchy ofthe class you're executing. When you derive your applet from Java's<TT>Applet</TT> class, for example, the interpreter needs to loadthe <TT>Applet</TT> class, as well as <TT>Applet</TT>'s superclasses,in order to run your application. Before the interpreter can accessthese class files, it has to know where they are.<P>Normally, when you run a program, the interpreter finds classesusing the current setting of your system's CLASSPATH variable,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>java </TT>command line). However, you can changethe setting of CLASSPATH temporarily for the current program runby using the <TT>-classpath</TT> option, like this:<BLOCKQUOTE><PRE>java -classpath path FileName</PRE></BLOCKQUOTE><P>In the preceding 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 runs your programusing the same settings the interpreter would use by default:<BLOCKQUOTE><PRE>java -classpath c:\java\lib\classes.zip;c:\classes FileName</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 interpreterto find the classes it needs to successfully run your applet.<H3><A NAME="SwitchingOnVerboseOutput">Switching On Verbose Output</A></H3><P>When you run the Java interpreter with no command-line option,the compiler runs and performs its task without displaying informationon the screen. Sometimes, though, you may want to know what filesthe interpreter is loading and where those files are being loadedfrom. You can make the interpreter report to you as it works byusing the <TT>-verbose</TT> option, like this:<BLOCKQUOTE><PRE>java -verbose applet.java</PRE></BLOCKQUOTE><H3><A NAME="ExampleRunninganApplicationwithVerboseOutput">Example: Running an Application with Verbose Output</A></H3><P>To see what happens when you use the <TT>-verbose</TT> (or <TT>-v</TT>)command-line option, copy the SimpleApp.class file from the CHAP35folder of this book's CD-ROM to your CLASSES folder. Then startan MS-DOS session and type the following command at the prompt:<BLOCKQUOTE><PRE>java -verbose SimpleApp</PRE></BLOCKQUOTE><P>When you press Enter, the interpreter runs, loading the applicationand displaying all the additional files it has to access in orderto run the application. Figure 35.3 shows a portion of this output.Bet you didn't expect such a simple program could make the interpreterwork so hard!<P><A HREF="f35-3.gif"><B> Figure 35.3 : </B><I>The -verbose option enables you to see what files are being loaded by the interpreter.</I></A><P><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>TIP</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>A special version of the Java interpreter can trace and display every command that's executed in your application. (You can find this tool in your JAVA\BIN folder, along with the other Java tools.) To invoke this special option, type <TT>java_g -t AppName</TT> at your system prompt. The <TT>AppName</TT> portion of the command is the name of the .CLASS file you want to run, without the file extension. Figure 35.4 shows a small portion of the output generated by this command. Even a small application results in many pages of trace information.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><A HREF="f35-4.gif"><B> Figure 35.4 : </B><I>The java_g -t command displays every command executed in your application.</I></A><P><H3><A NAME="GettingHelp">Getting Help</A></H3><P>The Java interpreter has a long list of command, so you'll probablyhave a hard time remembering them all. Luckily, you can get aquick reminder of what the commands are and what they do. To getthis information, type the following command:<BLOCKQUOTE><PRE>java -help</PRE></BLOCKQUOTE><P>When you do, you'll see the display shown in Figure 35.5. Formore information than is displayed in the help listing, checkthis chapter or your online Java documentation.<P><A HREF="f35-5.gif"><B> Figure 35.5 : </B><I>The Java interpreter features a built-in help display.</I></A><P><H2><A NAME="Summary"><FONT SIZE=5 COLOR=#Ff0000>Summary</FONT></A></H2><P>When you are using a Java-compatible Web browser to view applets,you don't need to be concerned with the Java interpreter. Thebrowser displays the applets for you automatically. However, ifyou want to run a standalone Java application, you must invokethe interpreter from your system's command line. The interpreteraccepts over a dozen different command-line options, althoughonly a few are regularly useful to novice and intermediate Javaprogrammers.<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#Ff0000>Review Questions</FONT></A></H2><OL><LI>What happens if you fail to include the file extension whenspecifying your source-code file to the interpreter?<LI>What extension do byte-code files-the files that the interpreterunderstands- have?<LI>What are the two ways you can specify the verbose interpreteroption?<LI>When writing a Java application, what do you use first, thecompiler or the interpreter?<LI>How can you get a list of commands that the interpreter understands?<LI>Why do you need to use an interpreter?<LI>What does the <TT>-checksource</TT> command-line option do?</OL><H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#Ff0000>Review Exercises</FONT></A></H2><OL><LI>Run one of the applications from <A HREF="ch32.htm" >Chapter 32</A>, instructing theinterpreter to check whether the .CLASS file is up to date withthe .java file.<LI>Run an application with the verbose setting, and study theinformation the interpreter displays on the screen.</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 + -