搜索
熱搜: 活動 交友 discuz
查看: 1922|回復: 0
打印 上一主題 下一主題

[教學] The Java Tutorial Third Edition

[複製鏈接]
跳轉到指定樓層
1#
發表於 2007-7-27 21:19:59 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
Practise Typing & Typed By Stephen Chung From www.eyny.com

Getting Started

THIS chapter gives a quick introduction to the Java technology. First, we explain what the Java platform is and what it can do. Next are step-by-step instructions on how to compile and run two simple programs on the Win32, the UNIX/Linux or the MacOS platforms. After that, we take a look at the code for the two programs, so you can see how they work. The chapter ends with questions and exercises to test and expand your knowledge, followed by a table of download instructions for the code used in this chapter.

The software development knits (SDKs) that Sun Microsystems provides include a minimal set of tools to let you run and compile your programs. Serious developers are advised to use a professional Integrated Development Environment (IDE). See Integrated Development Environments (page 540) for a list of IDEs.

About the Java Technology

Talk about Java technology seems to be everywhere, but what exactly is it? The next two sections explain how it is both a programming language and a platform.

The Java Programming Language

The Java programming language is a high-level language that can be characterized by all the following buzzwords:
When you’re finished, the dialog box should look like Figure 5. Now click Save and exit NotePad.

Compile the Souce File
From the Start menu, select the MS-DOS Prompt (Windows 95/98) or Command Prompt (Windows NT/2000) application. When the application launches, it should look like Figure 6.

The prompt shows your current directory. When you bring up the prompt for Windows 95/98, your current directory is usually WINDOWS on your C drive (as shown in Figure 6) or WINNT for Windows NT. To compile your source code file, changes your current directory to the one in which your file is located. For example, if your source directory is java on the C drive, you would type the following command at the prompt and press Enter:
cd c:\java

Now the prompt should change to C;\java. To change to a directory on a different drive, you must type an extra command. As shown in Figure 7, to change to the java directory on the D drive, you must reenter the drive, d:.

In our example, the java directory is on the C drive. The dir command lists the files in your current directory. If your current directory is C:\java and your enter dir at the prompt, you should see your file (Figure 8).

Now You can compile. At the prompt, type the following command and press Enter:
        Javac HelloWorldApp.java
If your prompt reappears without error messages, congratulations. You have successfully compiles your program. If you encounter errors, see Error Explanation (Win32) to help you fix the problems.

The compiler has generated a Java bytecode file, HelloWorldApp.class. At the prompt, type dir to see the new file that was generated (Figure 9). Now that you have a .class file, you can run your program.

Run the program
In the same directory, enter the following command at prompt:
        Java HelloWorldApp

Figure 10 shows what you should see.

Creating Your First Applet
HelloWorldApp is an example of an application, a standalone program. Now you will create an applet called HelloWorld, which also displays the greeting “Hello World!” Unlike HelloWorldApp, however, the applet runs in a Java-enabled Web browse, such as the HotJava browser, Netscape Navigator, or Microsoft Internet Explorer.

To create this applet, you’ll perform the basic steps as before: create a source file, compile the source file, and run the program. However, unlike for an appkication, you must also create and HTML file.

Create a Source File
You have two options to create a source file. You can save the files HelloWorld.java and Hello.html on your computer and avoid a lot of typing. Then you can go straight to the second step of compiling the source file (page 14). Or, youfollow these instructions.

First, start NotePad. In a new document, type the following code:

import java.applet.Applet;
import java.awt.Graphics;

public class HelloWorld extens Applet {
        public void paint(Graphics g) {
        // Display “Hello world!”
        g.drawString(“Hello world!”, 50, 25);
        }
}

Save this code to a file called HelloWorld.java.

Second, you also need an HTML file to accompany your applet. Type the following code into a new NotePad document:

<HTML>
        <HEAD>
                <TITLE>A Simple Program</TITLE>
        </HEAD>
        <BODY>
                Here is the output of my program:
                <APPLET CODE=”HelloWorld.class” WIDTH=150 HEIGHT=25>
                </APPLET>
        </BODY>
</HTML>

Save this code to a file called Hello.html.

Compile the Source File
At the prompt, type the following command and press Return:
        Javac HtlloWorld.java

The compiler should generate a Java bytecode file, HelloWorld.class.

Run the Program
Although you can use a Web browser to view your applets, you may find it easier to test your view the HelloWorld applet using appletviewer, enter at the prompt:
        Appletviewer Hello.html
Figure 11 shows what you should see.

Congratulatoins! Your applet works. If you encounter error, see Common Problems and Their Solutions (page 391) to help you fix the problems.


Error Explanations (Win32)
Here we list the most common errors  users have when compiling and running their first application or applet using the Java 2 SDK or an earlier JDK. For more error explanations, consult the section Common Problems and Their Solutions (page 391).

Bad command or file name (Windows 95/98)

The name specified is not recognized as an internal or external command, operable program or batch file (Windows NT/2000)
        If you receive this error, Windows cannot find the java compiler, javac. Here’s one way to tell Windows where to find javac. Suppose that you installed the Java 2 Software Development Kit in C:\jdk1.3. At the prompt, you would type the following command and press Enter:
        C:\jdk1.3.\bin javac HelloWorldApp.java

Note: If you choose this option, each time you compile or run a program, you must precede your javac and java commands with c:\jdk1.3\bin or the directory where you saved the Java 2 SDK, followed by \bin. The bin directory contains the compiler and interpreter. To avoid this extra typing, consult the section Update the PATH Variable (Win32 ) (page541).

Exception in thread “main” java.lang.NoClassDefFoundError: HelloWorldApp
If you receive this error, the interpreter cannot find your bytecode file, HelloWorldApp.class.
One of the places java tries to find your bytecode file is your current directory. So, if your bytecode file is in C, you should change your current directory to that. To change your directory, type the following command at the prompt and press Enter:
        cd c:
The prompt should change to C:. If you enter dir at the prompt, you should see your .java and .class files. Now enter java HelloWorldApp again.
If you still have problems, you might have change your CLASSPATH variable. To see whether this necessary, try clobbering the class path with the following command:
        Set CLASSPATH=
Now enter java HelloWorldApp again. If the program works now, you’ll have to change your CLASSPATH variable. For more information, consult the section Path Help ( page 540).
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

本論壇為非營利之網路平台,所有文章內容均為網友自行發表,不代表論壇立場!若涉及侵權、違法等情事,請告知版主處理。


Page Rank Check

廣告刊登  |   交換連結  |   贊助我們  |   服務條款  |   免責聲明  |   客服中心  |   中央分站

手機版|中央論壇

GMT+8, 2024-4-29 15:16 , Processed in 0.181283 second(s), 16 queries .

Powered by Discuz!

© 2005-2015 Copyrights. Set by YIDAS

快速回復 返回頂部 返回列表