📄 compile.html
字号:
Here is the <A HREF="./Code/ExampleProgram.java">ExampleProgram.java</A>source code file if you do not want to type the program text in yourself.<A NAME="comp"></A><H3>Compiling the Program</H3>A program has to be converted to a form the Java VM can understandso any computer with a Java VM can interpret and run the program. Compiling a Java program means taking the programmer-readable text in your program file (also called source code) and converting it to bytecodes, which are platform-independent instructions for the Java VM.<P>The Java compiler is invoked at the command line on Unix and DOS shell operating systems as follows:</FONT><PRE> javac ExampleProgram.java</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><BLOCKQUOTE><HR><STRONG>Note:</STRONG>Part of the configuration process for setting up the Java platformis setting the class path. The class path can be set using eitherthe <CODE>-classpath</CODE> option with the <CODE>javac</CODE>compiler command and <CODE>java</CODE> interpreter command,or by setting the <CODE>CLASSPATH</CODE> environment variable. You need to set the class path to point to the directory where the<CODE>ExampleProgram</CODE> class is so the compiler and interpreter commands can find it. See<A HREF="http://java.sun.com/products/jdk/1.2/docs/tooldocs/tools.html">Java 2SDK Tools</A> for more information. <HR></BLOCKQUOTE><A NAME="run"></A><H3>Interpreting and Running the Program</H3>Once your program successfully compiles into Java bytecodes, you can interpret and run applications on any Java VM, or interpret andrun applets in any Web browser with a Java VM built in such asNetscape or Internet Explorer. Interpreting and runninga Java program means invoking the Java VM byte code interpreter, which converts the Java byte codes to platform-dependent machine codes so your computer can understand and run the program.<P>The Java interpreter is invoked at the command line on Unix and DOS shell operating systems as follows:</FONT><PRE> java ExampleProgram</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif">At the command line, you should see:</FONT><PRE> I'm a Simple Program</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Here is how the entire sequence looks in a terminal window:<P><IMG SRC="./Art/examprog.gif" WIDTH="196" HEIGHT="106" ALT=""><P><A NAME="debug"></A><H3>Common Compiler and Interpreter Problems</H3>If you have trouble compiling or running the simple examplein this lesson, refer to the <A HREF="http://java.sun.com/docs/books/tutorial/getStarted/problems/index.html">Common Compiler and Interpreter Problems</A> lesson in <A HREF="http://java.sun.com/docs/books/tutorial/trailmap.html">The Java Tutorial</A> for troubleshooting help.<A NAME="comm"></A><H3>Code Comments</H3>Code comments are placed in source files to describewhat is happening in the code to someone who might bereading the file, to comment-out lines of code to isolatethe source of a problem for debugging purposes, or to generateAPI documentation. To these ends, the Java language supports threekinds of comments: double slashes, C-style, and doc comments.<H4>Double Slashes</H4>Double slashes (<code>//</code>) are used in the C++ programminglanguage, and tell the compiler totreat everything from the slashes to the end of the line astext.</FONT><PRE>//A Very Simple Exampleclass ExampleProgram { public static void main(String[] args){ System.out.println("I'm a Simple Program"); }}</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><H4>C-Style Comments</H4>Instead of double slashes, you can use C-stylecomments (<code>/* */</code>) to enclose one ormore lines of code to be treated as text.</FONT><PRE>/* These areC-style comments*/class ExampleProgram { public static void main(String[] args){ System.out.println("I'm a Simple Program"); }}</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><H4>Doc Comments</H4>To generate documentation for your program, use the doc comments(<code>/** */</code>) to enclose lines of text for the<code>javadoc</code> tool to find. The <code>javadoc</code> toollocates the doc comments embeddedin source files and uses those comments to generate API documentation.</FONT><PRE>/** This class displays a text string at* the console.*/class ExampleProgram { public static void main(String[] args){ System.out.println("I'm a Simple Program"); }}</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif">With one simple class, there is noreason to generate API documentation. API documentationmakes sense when you have an application made up of a numberof complex classes that need documentation. The tool generates<CODE>HTML</CODE> files (Web pages) that describe the class structures and containthe text enclosed by doc comments.The <A HREF="http://java.sun.com/products/jdk/javadoc/index.html">javadocHome Page</A> has more information on the <CODE>javadoc</CODE> commandand its output.<A NAME="api"></A><H3>API Documentation</H3>The Java platform installation includes API Documentation, whichdescribes the APIs available for you to use in your programs.The files are stored in a <CODE>doc</CODE> directory beneaththe directory where you installed the platform. For example,if the platform is installed in <CODE>/usr/local/java/jdk1.2</CODE>,the API Documentation is in <CODE>/usr/local/java/jdk1.2/doc/api</CODE>.<A NAME="more"></A><H3>More Information</H3>See<A HREF="http://java.sun.com/products/jdk/1.2/docs/tooldocs/tools.html">Java 2SDK Tools</A> for more information on setting the class path and usingthe <CODE>javac</CODE>, and <CODE>java</CODE> commands.<P>See<A HREF="http://java.sun.com/docs/books/tutorial/getStarted/problems/index.html">Common Compiler and Interpreter Problems</A> lesson in<A HREF="http://java.sun.com/docs/books/tutorial/trailmap.html">The JavaTutorial</A> for troubleshooting help.<P>The <A HREF="http://java.sun.com/products/jdk/javadoc/index.html">javadocHome Page</A> has more information on the <CODE>javadoc</CODE> commandand its output.<P>You can also view the API Documentation for the Java 2 Platform on the <A HREF="http://java.sun.com/products/jdk/1.2/docs/api/index.html">java.sun.com</A> site. <P>_______<BR><A NAME="TJVM"><SUP>1</SUP></A> As used on this web site, the terms "Java virtual machine" or "JVM" mean a virtual machine for the Java platform.<P ALIGN="RIGHT"><FONT SIZE="-1">[<A HREF="#top">TOP</A>]</FONT></FONT></TD></TR></TABLE><!-- ================ --><!-- End Main Content --><!-- ================ --></FONT></TD></TR></TABLE><!-- Copyright Insert --><BR CLEAR="ALL"><FORM ACTION="/cgi-bin/search.cgi" METHOD="POST"><TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="5"> <TR> <TD VALIGN="BOTTOM"></TD></TR><A HREF="/servlet/PrintPageServlet"><IMG SRC="/images/printbutton.gif" WIDTH="155" HEIGHT="25" ALT="Print Button" BORDER="0"></A> <CENTER> <FONT SIZE="-1" COLOR="#999999" FACE="Verdana, Arial, Helvetica, sans-serif"> [ This page was updated: <!-- new date --> 31-Mar-2000 ]</font></CENTER> </TD> </TR> <TR> <TD BGCOLOR="#CCCCCC"> <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR> <TD> <CENTER> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> <A HREF="http://java.sun.com/products/">Products & APIs</A> | <A HREF="/developer/index.html">Developer Connection</A> | <A HREF="/developer/infodocs/index.shtml">Docs & Training</A> | <A HREF="/developer/support/index.html">Online Support</A><BR> <A HREF="/developer/community/index.html">Community Discussion</A> | <A HREF="http://java.sun.com/industry/">Industry News</A> | <A HREF="http://java.sun.com/solutions">Solutions Marketplace</A> | <A HREF="http://java.sun.com/casestudies">Case Studies</A> </FONT> </CENTER> </TD> </TR> <TR> <TD BGCOLOR="#CCCCCC"> <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR> <TD ALIGN="CENTER"> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> <A HREF="http://java.sun.com/docs/glossary.html">Glossary</A> - <A HREF="http://java.sun.com/applets/">Applets</A> - <A HREF="http://java.sun.com/docs/books/tutorial/">Tutorial</A> - <A HREF="http://java.sun.com/jobs/">Employment</A> - <A HREF="http://java.sun.com/nav/business/">Business & Licensing</A> - <A HREF="http://java.sun.com/javastore/">Java Store</A> - <A HREF="http://java.sun.com/casestudies/">Java in the Real World</A> </FONT> </TD> </TR> <TR> <TD> <CENTER> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> <a href="/siteinfo/faq.html">FAQ</a> | <a href="/feedback/index.html">Feedback</a> | <a href="http://www.dynamicdiagrams.net/mapa/cgi-bin/help.tcl?db=javasoft&dest=http://java.sun.com/">Map</a> | <A HREF="http://java.sun.com/a-z/index.html">A-Z Index</A> </FONT> </CENTER> </TD> </TR> <TR> <TD> <TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="0"> <TR> <TD WIDTH="50%"> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> For more information on Java technology<BR> and other software from Sun Microsystems, call:<BR> </FONT> <FONT SIZE="-1" FACE="Verdana, Arial, Helvetica, sans-serif"> (800) 786-7638<BR></FONT> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> Outside the U.S. and Canada, dial your country's <A HREF="http://www.att.com/business_traveler/attdirecttollfree/">AT&T Direct Access Number</A> first.<BR> </FONT> </TD> <TD ALIGN="RIGHT" WIDTH="50%"> <A HREF="http://www.sun.com"><IMG SRC="/images/lgsun.gif" width="64" height="30" border="0" ALT="Sun Microsystems, Inc."></A><BR> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> Copyright © 1995-2000 <A HREF="http://www.sun.com">Sun Microsystems, Inc.</A><BR> All Rights Reserved. <A HREF="http://www.sun.com/share/text/termsofuse.html">Terms of Use</A>. <A HREF="http://www.sun.com/privacy/">Privacy Policy</A>. </FONT> </TD> </TR> </TABLE> </TD> </TR> </TABLE></FORM><!-- End Copyright Insert --></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -