First Steps (UNIX/Linux)

First Steps (UNIX Linux)

These instructions tell you how to compile and run your first programs on UNIX and Linux platforms. (Win32 instructions are on page 8. Users on MacOS platforms can find instructions on page 24.) We start with a checklist of what you need to write your first program. Next, we cover the steps to creating an application, steps to creating an applet, and explanations of error messages you may encounter.

A Checklist

To write your first program, you will need

  1. The Java 2 SDK, Standard Edition: The Java 2 SDK software is included on the CD that accompanies this book. You can download this SDK to your workstation or check http://java.sun.com/products/ for the latest version. [1]

    [1] The Linux platform was first supported in the Java 2 SDK, Standard Edition v. 1.3 release. Before version 1.2, the software development kit provided by Sun Microsystems was called the "JDK."

  2. A text editor: In this example, we'll use Pico, an editor available on 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 program.

Creating Your First Application

Your first program, HelloWorldApp, will simply display the greeting "Hello World!" To create this program, you will complete each of the following steps.

Create a Source File

You have two options. You can save the file HelloWorldApp.java [1] on your computer and avoid a lot of typing. Then you can go straight to the second step of compiling the file (page 18). Or, you can follow these (longer) instructions.

[1] Throughout this book, we use the CD icon to indicate that the code (in this case, HelloWorldApp.java) is available on the CD and online. See Code Samples (page 43) for the file location on the CD and online.

First, open a shell, or "terminal," window (Figure 12).

Figure 12. A UNIX 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.

We recommend that you keep the files you create 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, you would first change your current directory to your home directory by entering the following command:

cd

Then you would enter the following command:

mkdir java

To change your current directory to this new directory, you would 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 , Pico is probably unavailable. Consult your system administrator for more information, or use another editor.

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

Type the following code into the new buffer:

/** * The HelloWorldApp class implements an application that * displays "Hello World!" to the standard output. */ public 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. The compiler and interpreter are case sensitive, so you must capitalize consistently. In other words, HelloWorldApp is not equivalent to helloworldapp.

Save the code with the name HelloWorldApp.java by typing Ctrl-O in your Pico editor. On the bottom line of your editor, you will see the prompt File Name to write. Enter HelloWorldApp.java, preceded by the directory where you want to create the file. For example, if /home/myname/ is your home directory and you want to save HelloWorldApp.java in the directory /home/myname/java, you would type /home/myname/java/HelloWorldApp.java and press Return. Type Ctrl-X to exit Pico.

Compile the Source File

Bring up another shell window. To compile your source file, change your current directory to the one in which your file is located. For example, if your source directory is /home/ myname/java, you would type the following command at the prompt and press Return: [1]

[1] You could also change to the source directory with two commands: Type cd, press Return, then type java and press Return. Typing cd alone changes you to your home directory (that is, to /home/myname).

cd /home/myname/java

You can type pwd at the prompt to see your current directory. In this example, the current directory has been changed to /home/myname/java. If you enter ls at the prompt, you should see your file listed (Figure 13).

Figure 13. The HelloWorldApp.java file listed in the current directory.

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 successfully compiled your program. If you encounter errors, see Common Problems and Their Solutions (page 391) to help you fix the problems.

The compiler has generated a bytecode file, HelloWorldApp.class. At the prompt, type ls to see the new file (Figure 14).

Figure 14. Compiling HelloWorld.java creates the bytecode file, HelloWorldApp.class, in the same directory.

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

Run the Program

In the same directory, enter at the prompt: java HelloWorldApp. Figure 15 shows what you should see.

Figure 15. Running the HelloWorldApp program.

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 browser, 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 application, you must also create an HTML file.

Create a Source File

Again, you have two options. You can save the files HelloWorld.java and Hello.html [1] on your computer and avoid a lot of typing. Then you can go straight to Compile the Source File (page 21). Or, you can follow these instructions.

[1] HelloWorld.java and Hello.html are available on this book's CD and online. See Code Samples (page 43).

First, start Pico. Type the following code into a new buffer:

import java.applet.Applet; import java.awt.Graphics; public class HelloWorld extends Applet { public void paint(Graphics g) { // Display "Hello World!" g.drawString("Hello world!", 50, 25); } }

Save this code to a file named HelloWorld.java. Type Ctrl-X to exit Pico.

Second, you also need an HTML file to accompany your applet. Restart Pico and type the following code into a new buffer:

 

A Simple Program

Here is the output of my program:

Save this code to a file called Hello.html in the same directory as your .java file.

Compile the Source File

At the prompt, type the following command and press Return:

javac HelloWorld.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 applets by using the simple appletviewer application that comes with the Java 2 SDK. To view the HelloWorld applet using appletviewer, enter at the prompt:

appletviewer Hello.html

Figure 16 shows what you should see. Congratulations! Your applet works. If you encounter errors, see Common Problems and Their Solutions (page 391) to help you fix the problems.

Figure 16. The successful execution of the HelloWorld applet.

Error Explanations (UNIX/Linux)

Here we list the most common errors users have when compiling and running their first application or applet. For more error explanations, consult the section Common Problems and Their Solutions (page 391).

javac: Command not found

If you receive this error, the operating system cannot find the Java compiler, javac. Here's one way to tell it where to find javac. Suppose that you installed the Java 2 Software Development Kit in /usr/local/jdk1.3. At the prompt, you would type the following command and press Return:

/usr/local/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 /usr/local/jdk1.3/bin. To avoid this extra typing, consult the section Update the PATH Variable (UNIX) (page 543) in the Appendix.

 

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 the interpreter tries to find your bytecode file is your current directory. So, if your bytecode file is in /home/myname/java/, you should change your current directory to that directory. To change your directory, type the following command at the prompt and press Return:

cd /home/myname/java

Type pwd at the prompt; you should see /home/myname/java. If you type 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 whether this is 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).

Категории