站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Java Tutorial 5.0 英文版

"Hello World" for UNIX - Java Tutorial 5.0 英文版

The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Getting Started
Lesson: The "Hello World" Application

"Hello World" for UNIX

It's time to write your first application! These detailed instructions are for users of UNIX-based platforms, including Linux and Solaris. Instructions for other platforms are in "Hello World" for Microsoft Windows (in the Getting Started trail) and "Hello World" for Mac OS X (in the Getting Started trail).


A Checklist 

To write your first program, you need:
  1. The J2SETM Development Kit 5.0 (JDKTM 5.0). You can download the Solaris or Linux version now (outside of the tutorial). (Make sure you download the JDK, not the JRE.) Consult the Solaris installation instructions (outside of the tutorial) or the Linux installation instructions (outside of the tutorial).
  2. A text editor. In this example, we'll use Pico, an editor available for many UNIX-based platforms. You can easily adapt these instructions if you use a different text editor, such as Vi or Emacs.

These two items are all you need to write your first application.


Note: Consider using an Integrated Development Environment (IDE) to help you write your programs. JDK 5.0 is available bundled with the NetBeans IDE. You can download this bundle from the JDK 5.0 download page (outside of the tutorial).

Creating Your First Application

Your first application, HelloWorldApp, will simply display the greeting "Hello world!". To create this program, you will: 

  • Create a source file. A source file contains text, written in the Java programming language, that you and other programmers can understand. You can use any text editor to create and edit source files.

  • Compile the source file into a .class file. The Java compiler, javac, takes your source file and translates its text into instructions that the Java Virtual Machine can understand. The instructions contained within this .class file are known as bytecodes.

  • Run the program. The Java launcher (java) uses the Java Virtual Machine to run your application.

top


Create a Source File

To create a source file, you have two options:

First, open a shell, or "terminal," window.

A new terminal window

A new terminal window

When you first bring up the prompt, your current directory will usually be your home directory. You can change your current directory to your home directory at any time by typing cd at the prompt and then pressing Return.

The source files you create should be kept in a separate directory. You can create a directory by using the command mkdir. For example, to create the directory java in your home directory, use the following commands:

            
cd
mkdir java

To change your current directory to this new directory, you then enter:

cd java

Now you can start creating your source file.

Start the Pico editor by typing pico at the prompt and pressing Return. If the system responds with the message pico: command not found, then Pico is most likely unavailable. Consult your system administrator for more information, or use another editor.

When you start Pico, it'll display a new, blank buffer. This is the area in which you will type your code.

 
Pico? Vi? Emacs?

Pico is probably the easiest of the three editors to use. If you're curious about how to use the other editors, however, check out these handy reference cards for Vi (outside of the tutorial) and Emacs (outside of the tutorial).

Type the following code into the new buffer:

/**
 * The HelloWorldApp class implements an application that
 * simply displays "Hello World!" to the standard output.
 */
class HelloWorldApp {
    public static void main(String[] args) {
        //Display "Hello World!"
        System.out.println("Hello World!");
    }
}
 Be Careful When You Type

Type all code, commands, and file names exactly as shown. Both the compiler (javac) and launcher (java) are case-sensitive, so you must capitalize consistently.

HelloWorldApp helloworldapp

Save the code in a file with the name HelloWorldApp.java. In the Pico editor, you do this by typing Ctrl-O and then, at the bottom where you see the prompt File Name to write:, entering the directory in which you wish to create the file, followed by HelloWorldApp.java. For example, if you wish to save HelloWorldApp.java in the directory /home/rortigas/java, then you type /home/rortigas/java/HelloWorldApp.java and press Return.

You can type Ctrl-X to exit Pico.

top

Compile the Source File

Bring up another shell window. To compile your source file, change your current directory to the directory where your file is located. For example, if your source directory is /home/rortigas/java, type the following command at the prompt and press Return:

cd /home/rortigas/java

If you enter pwd at the prompt, you should see the current directory, which in this example has been changed to /home/rortigas/java.

If you enter ls at the prompt, you should see your file.

Results of the ls command, showing the .java source file

Results of the ls command, showing the .java source file

Now you can compile. At the prompt, type the following command and press Return.

javac HelloWorldApp.java

If your prompt reappears without error messages, congratulations. You have successfully compiled your program.

Error Explanation

javac: Command not found

If you receive this error, UNIX cannot find the Java compiler, javac.

Here's one way to tell UNIX where to find javac. Suppose you installed the JDK in /usr/local/jdk5.0. At the prompt you would type the following command and press Return:

/usr/local/jdk5.0/javac HelloWorldApp.java
Note: If you choose this option, each time you compile or run a program, you'll have to precede your javac and java commands with /usr/local/jdk5.0/. To avoid this extra typing, you could add this information to your PATH variable. The steps for doing so will vary depending on which shell you are currently running.

The compiler has generated a Java bytecode file, HelloWorldApp.class. At the prompt, type ls to see the new file that was generated, as shown in the following figure.

Results of the ls command, showing the generated .class file

Results of the ls command, showing the generated .class file

Now that you have a .class file, you can run your program.

top

Run the Program

In the same directory, enter at the prompt:
java HelloWorldApp

The next figure shows what you should now see.

The output prints

The output prints "Hello World!" to the screen.

Congratulations! Your program works.

Error Explanation

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp

If you receive this error, java cannot find your bytecode file, HelloWorldApp.class.

One of the places java tries to find your bytecode file is your current directory. So, for example, if your bytecode file is in /home/rortigas/java, you should change your current directory to that. To change your directory, type the following command at the prompt and press Return:

cd /home/rortigas/java

If you enter pwd at the prompt, you should see /home/rortigas/java. If you enter ls at the prompt, you should see your .java and .class files. Now enter java HelloWorldApp again.

If you still have problems, you might have to change your CLASSPATH variable. To see if this is necessary, try clobbering the classpath with the following command.

unset CLASSPATH

Now enter java HelloWorldApp again. If the program works now, you'll have to change your CLASSPATH variable in the same manner as the PATH variable above.

top

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.