HelloWorld Application

   

When you're learning a new programming language, it is often useful to jump in headfirst, learn how to write a short application, and then step back and learn the rest of the fundamentals. In this chapter, you do just that, and by the end you'll be able to write simple Java programs of your own.

Let's get started with an example application. Listing 2.1 shows the complete source code for the well-known HelloWorld program as written in Java.

Listing 2.1 The HelloWorld Application
 /** * My first program demonstrates displaying * a string on the screen */ public class HelloWorld {   public static void main(String args[]){     System.out.println("Hello World!");   } } 

As you can see, there really isn't a lot to this program, which makes it a good place to start. The syntax might look unfamiliar to you at this point, but you can probably gather by looking at the code that the purpose of this program, just like any other HelloWorld program, is to write a simple message to the screen. You'll see the details of the code later, but first follow the steps in the next few sections to create your own copy of the source file and compile and run the program.

Create the File

The first step in creating the HelloWorld application is to copy the text from Listing 2.1 into a file called HelloWorld.java using your favorite text editor (Windows Notepad, or SimpleText on the Macintosh will work if you don't have another). It is very important to call the file HelloWorld.java, because the compiler expects the filename to match the class identifier (see "Declaring a Class" later in this chapter). Java is case sensitive, so you must also be careful to name the file HelloWorld.java and not helloworld.java, HELLOWORLD.JAVA, or any other variation.

Caution

If you use a program such as Microsoft Word to create your source file, make sure you save the file as text only. If you save it as a Word document, the compiler will have problems with the extra formatting information in the file.


Compile the Code

To compile the program, you must have the SDK installed. If you do not, refer to Appendix A, "Installing the SDK and Getting Started," for step-by-step instructions on obtaining a current copy of the SDK and configuring your development environment. You can then use the javac program to compile your source file into a form that the Java virtual machine can load and execute. To run javac on a Macintosh, drag the source file over the javac icon. On any other computer, type the following at a command prompt set to the directory containing the source file:

 javac HelloWorld.java 

The javac program creates a file called HelloWorld.class from the HelloWorld.java file. HelloWorld.class is referred to as a class file. A class file represents one or more Java classes and contains bytecodes, which are instructions that can be run by the Java interpreter. If you get a compiler error when running javac, go back and make sure you typed in the file exactly as it is shown in Listing 2.1, and make sure that you named the file HelloWorld.java.

See Appendix A, "Installing the SDK and Getting Started,"

Run the Program

Now that you have compiled the program, you can run it by typing the following at the command prompt:

 java HelloWorld 

Note

The HelloWorld used in the command java HelloWorld is not HelloWorld.class or HelloWorld.java, just the name of the class.


After you do this, the computer should print the following to the screen:

 Hello World! 

Troubleshooting Tip

If instead of seeing the Hello World! message, you get a java.lang.NoClassDefFoundError , you might have a problem with your classpath. See " NoClassDefFoundError " in the "Troubleshooting" section at the end of this chapter for help in resolving this error.


   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net