⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 your first cup of java (for win32).mht

📁 Your First Cup of Java
💻 MHT
📖 第 1 页 / 共 5 页
字号:
src=3D"http://java.sun.com/docs/books/tutorial/figures/getStarted/cd.gif"=
=20
          width=3D528 align=3Dbottom NATURALSIZEFLAG=3D"3"></CENTER>
          <P></P>
          <P>Now you can compile. At the prompt, type the following =
command and=20
          press <FONT color=3D#990099>Enter</FONT>:</P>
          <BLOCKQUOTE>
            <TABLE cellSpacing=3D2 cellPadding=3D0 border=3D1>
              <TBODY>
              <TR>
                <TD width=3D"100%"><CODE>&nbsp;javac=20
                  =
HelloWorldApp.java&nbsp;</CODE></TD></TR></TBODY></TABLE></BLOCKQUOTE>
          <P>If your prompt reappears without error messages, =
congratulations.=20
          You have successfully compiled your program.</P>
          <BLOCKQUOTE>&nbsp;=20
            <TABLE cellSpacing=3D0 cellPadding=3D10 width=3D550 =
border=3D1>
              <TBODY>
              <TR bgColor=3D#ffcccc>
                <TD bgColor=3D#ffdddd><B><FONT=20
                  face=3D"Arial, Helvetica, sans-serif"><FONT =
color=3D#804000><FONT=20
                  size=3D+1>Error Explanation</FONT></FONT></FONT></B>=20
                  <P><B><CODE>Bad command or file name</CODE></B> =
<I>(Windows=20
                  95/98)</I>=20
                  <P><B><CODE>The name specified is not recognized as an =

                  internal or external command, operable program or =
batch=20
                  file</CODE></B> <I>(Windows NT)</I>=20
                  <P>If you receive this error, Windows cannot find the =
Java=20
                  compiler, <CODE>javac</CODE>.=20
                  <P>Here's one way to tell Windows where to find=20
                  <CODE>javac</CODE>. Suppose you installed the Java 2 =
Software=20
                  Development Kit in <CODE>C:\jdk1.4</CODE>. At the =
prompt you=20
                  would type the following command and press =
<B>Enter</B>:&nbsp;=20

                  <BLOCKQUOTE><PRE><CODE>C:\jdk1.4\bin\javac =
HelloWorldApp.java</CODE></PRE></BLOCKQUOTE><B>Note:=20
                  </B>If you choose this option, each time you compile =
or run a=20
                  program, you'll have to precede your =
<CODE>javac</CODE> and=20
                  <CODE>java</CODE> commands with =
<CODE>C:\jdk1.4\bin\</CODE>.=20
                  To avoid this extra typing, consult the section <I><A=20
                  =
href=3D"http://java.sun.com/j2se/1.4/install-windows.html#Environment"=20
                  target=3Dother><FONT color=3D#009bbb>Update the PATH=20
                  variable</FONT></A><A=20
                  =
href=3D"http://java.sun.com/j2se/1.4/install-windows.html#Environment"><I=
MG=20
                  height=3D11 alt=3D"(outside of the tutorial)"=20
                  =
src=3D"http://java.sun.com/docs/books/tutorial/images/otherIcon.gif"=20
                  width=3D11 align=3DabsMiddle border=3D0></A> </I>in =
the installation=20
                  instructions.</TD></TR></TBODY></TABLE></BLOCKQUOTE>
          <P><BR>The compiler has generated a Java bytecode file,=20
          <CODE>HelloWorldApp.class</CODE>. At the prompt, type =
<CODE>dir</CODE>=20
          to see the new file that was generated:=20
          <P>
          <CENTER><IMG height=3D248 alt=3D""=20
          =
src=3D"http://java.sun.com/docs/books/tutorial/figures/getStarted/dosclas=
s.gif"=20
          width=3D532 align=3Dbottom NATURALSIZEFLAG=3D"3"></CENTER>
          <P></P>Now that you have a <CODE>.class</CODE> file, you can =
run your=20
          program.</BLOCKQUOTE><IMG height=3D16 alt=3D""=20
        =
src=3D"http://java.sun.com/docs/books/tutorial/figures/getStarted/arrow.g=
if"=20
        width=3D13><FONT size=3D-1><A=20
        =
href=3D"http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32=
.html#top">top</A></FONT>=20
        <BR>
        <HR align=3Dleft width=3D"70%">

        <H2><A name=3D2c></A><FONT face=3D"Arial, Helvetica, =
sans-serif"><FONT=20
        color=3D#ff0000>c.</FONT> Run the Program.</FONT></H2>
        <BLOCKQUOTE>In the same directory, enter at the prompt:=20
          <BLOCKQUOTE>
            <TABLE cellSpacing=3D2 cellPadding=3D0 border=3D1>
              <TBODY>
              <TR>
                <TD width=3D"100%"><CODE>&nbsp;java=20
              =
HelloWorldApp&nbsp;</CODE></TD></TR></TBODY></TABLE></BLOCKQUOTE>Now=20
          you should see:=20
          <P>
          <CENTER><IMG height=3D131 alt=3D""=20
          =
src=3D"http://java.sun.com/docs/books/tutorial/figures/getStarted/dosresu=
lt.gif"=20
          width=3D484 align=3Dbottom NATURALSIZEFLAG=3D"3"></CENTER>
          <P></P>Congratulations! Your program works. </BLOCKQUOTE>
        <BLOCKQUOTE>
          <BLOCKQUOTE>&nbsp;=20
            <TABLE cellSpacing=3D0 cellPadding=3D10 width=3D550 =
border=3D1>
              <TBODY>
              <TR bgColor=3D#ffcccc>
                <TD bgColor=3D#ffdddd><A name=3Derror></A><B><FONT=20
                  face=3D"Arial, Helvetica, sans-serif"><FONT =
color=3D#804000><FONT=20
                  size=3D+1>Error Explanation</FONT></FONT></FONT></B>=20
                  <P><B><CODE>Exception in thread "main"=20
                  java.lang.NoClassDefFoundError: =
HelloWorldApp</CODE></B>=20
                  <P>If you receive this error, <CODE>java</CODE> cannot =
find=20
                  your bytecode file, <CODE>HelloWorldApp.class</CODE>.=20
                  <P>One of the places <CODE>java</CODE> tries to find =
your=20
                  bytecode file is your current directory. So, if your =
bytecode=20
                  file is in <CODE>C:\java</CODE>, you should change =
your=20
                  current directory to that. To change your directory, =
type the=20
                  following command at the prompt and press =
<B>Enter</B>:=20
                  <BLOCKQUOTE><PRE><CODE>cd =
c:\java</CODE></PRE></BLOCKQUOTE>The prompt=20
                  should change to <CODE>C:\java&gt;</CODE>. If you =
enter=20
                  <CODE>dir</CODE> at the prompt, you should see your=20
                  <CODE>.java</CODE> and <CODE>.class</CODE> files. Now =
enter=20
                  <CODE>java HelloWorldApp</CODE> again.=20
                  <P>If you still have problems, you might have to =
change your=20
                  CLASSPATH variable. To see if this is necessary, try=20
                  "clobbering" the classpath with the following command: =

                  <BLOCKQUOTE><PRE><CODE>set =
CLASSPATH=3D</CODE></PRE></BLOCKQUOTE>Now enter=20
                  <CODE>java HelloWorldApp</CODE> again. If the program =
works=20
                  now, you'll have to change your CLASSPATH variable. =
For more=20
                  information, consult the section <I><A=20
                  =
href=3D"http://java.sun.com/j2se/1.4/install-windows.html"=20
                  target=3Dother><FONT color=3D#009bbb>5. Update the =
PATH=20
                  variable</FONT></A><A=20
                  =
href=3D"http://java.sun.com/j2se/1.4/install-windows.html"><IMG=20
                  height=3D11 alt=3D"(outside of the tutorial)"=20
                  =
src=3D"http://java.sun.com/docs/books/tutorial/images/otherIcon.gif"=20
                  width=3D11 align=3DabsMiddle border=3D0></A> </I>in =
the installation=20
                  =
instructions.</TD></TR></TBODY></TABLE></BLOCKQUOTE></BLOCKQUOTE><IMG=20
        height=3D16 alt=3D""=20
        =
src=3D"http://java.sun.com/docs/books/tutorial/figures/getStarted/arrow.g=
if"=20
        width=3D13><FONT size=3D-1><A=20
        =
href=3D"http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32=
.html#top">top</A></FONT>=20

        <HR align=3Dleft>

        <H2><A name=3D3></A><FONT face=3D"Arial, Helvetica, =
sans-serif"><FONT=20
        color=3D#990099>3.</FONT><FONT color=3D#009900> </FONT>Creating =
Your First=20
        Applet</FONT></H2>
        <P><CODE>HelloWorldApp</CODE> is an example of a Java=20
        <I>application</I>, a standalone program. Now you will create a =
Java=20
        <I>applet </I>called <CODE>HelloWorld</CODE>, which also =
displays the=20
        greeting "Hello world!". Unlike <CODE>HelloWorldApp</CODE>, =
however, the=20
        applet runs in a Java-enabled Web browser such as HotJava, =
Netscape=20
        Navigator, or Microsoft Internet Explorer.</P>
        <P>To create this applet, you'll perform the basic steps as =
before:=20
        create a Java source file; compile the source file; and run the=20
        program.</P>
        <HR align=3Dleft width=3D"70%">

        <H2><FONT face=3D"Arial, Helvetica, sans-serif"><FONT =
color=3D#990099><A=20
        name=3D3a></A><FONT face=3D"Arial, Helvetica, sans-serif"><FONT=20
        color=3D#990099></FONT></FONT>a. </FONT>Create a Java Source=20
        File.</FONT></H2>
        <BLOCKQUOTE>
          <P>Again, you have two options:</P>
          <UL>
            <LI>You can save the files <A=20
            =
href=3D"http://java.sun.com/docs/books/tutorial/getStarted/applet/example=
/HelloWorld.java"=20
            target=3Dsource><FONT=20
            color=3D#bb000f><CODE>HelloWorld.java</CODE></FONT></A><A=20
            =
href=3D"http://java.sun.com/docs/books/tutorial/getStarted/applet/example=
/HelloWorld.java"><IMG=20
            height=3D11 alt=3D"(in a .java source file)"=20
            =
src=3D"http://java.sun.com/docs/books/tutorial/images/sourceIcon.gif"=20
            width=3D11 align=3DabsMiddle border=3D0></A> and <A=20
            =
href=3D"http://java.sun.com/docs/books/tutorial/getStarted/applet/example=
/Hello.html"><CODE>Hello.html</CODE></A>=20
            on your computer and avoid a lot of typing. Then, you can go =

            straight to <A=20
            =
href=3D"http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32=
.html#3b">step=20
            b</A>.<BR><BR>
            <LI>Or, you can follow these instructions: </LI></UL>
          <P><B><FONT color=3D#990099>1.</FONT></B><FONT =
color=3D#990099>=20
          </FONT>Start NotePad. Type the following code into a new =
document:=20
</P>
          <TABLE cellSpacing=3D2 cellPadding=3D2 width=3D500 border=3D1>
            <TBODY>
            <TR>
              <TD width=3D"100%"><PRE><CODE>import java.applet.*;
import java.awt.*;
=20
/**
 * The HelloWorld class implements an applet that
 * simply displays "Hello World!".
 */
public class HelloWorld extends Applet {
    public void paint(Graphics g) {
        // Display "Hello World!"
        g.drawString("Hello world!", 50, 25);
    }
}
</PRE></CODE></TD></TR></TBODY></TABLE>
          <P>Save this code to a file called =
<CODE>HelloWorld.java</CODE>.</P>
          <P><B><FONT color=3D#990099>2.</FONT> </B>You also need an =
HTML file to=20
          accompany your applet. Type the following code into a new =
NotePad=20
          document:</P>
          <TABLE cellSpacing=3D2 cellPadding=3D2 width=3D500 border=3D1>
            <TBODY>
            <TR>
              <TD width=3D"100%"><PRE><CODE>&lt;HTML&gt;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -