There are two methods for compiling and launching a Java program: from the command line, or from another program, such as an integrated development environment or a text editor. Let us do it the hard way first: from the command line. Open a shell or terminal window. Go to the CoreJavaBook/v1ch2/Welcome directory. Then enter the following commands:

javac Welcome.java java Welcome

You should see the message shown in on the screen.

Compiling and running Welcome.java

Java graphics 02fig01

Congratulations! You have just compiled and run your first Java program. What happened? The javac program is the Java compiler. It compiles the file Welcome.java into the file Welcome.class. The java program is the Java interpreter. It interprets the bytecodes that the compiler placed in the class file. The Welcome program is extremely simple. It merely prints a message to the console. You may enjoy looking inside the program shown in -we will explain how it works in the next chapter.

Example Welcome.java

 1. public class Welcome
 2. {
 3. public static void main(String[] args)
 4. {
 5. String[] greeting = new String[3];
 6. greeting[0] = "Welcome to Core Java";
 7. greeting[1] = "by Cay Horstmann";
 8. greeting[2] = "and Gary Cornell";
 9.
10. for (int i = 0; i < greeting.length; i++)
11. System.out.println(greeting[i]);
12. }
13. }

Troubleshooting Hints

In the age of visual development environments, many programmers are unfamiliar with running programs in a shell window. There are any number of things that can go wrong and lead to frustrating results. Pay attention to the following points:

Java graphics exclamatory_icon

There is an excellent tutorial at that goes into much greater detail about the "gotchas" that beginners can run into.