Java ScreenShot

Screenshot Core Java 2: Volume I - Fundamentals

Table of Contents
 2.  The Java Programming Environment

Using the Command Line Tools

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 java Welcome

You should see the message shown in Screenshot-1 on the screen.

Screenshot-1. Compiling and running

Java graphics 02fig01.gif

Congratulations! You have just compiled and run your first Java program. What happened? The javac program is the Java compiler. It compiles the file 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 Example 2-1—we will explain how it works in the next chapter.

Example 2-1
 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";
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:

  • If you type in the program by hand, make sure you pay attention to uppercase and lowercase letters. In particular, the class name is Welcome and not welcome or WELCOME.
  • The compiler requires a file name The interpreter requires a class name Welcome without a .java or .class extension.
  • If you get a message such as "Bad command or file name" or "javac: command not found," then you need to go back and double-check your installation, in particular the execution path setting.
  • If javac reports an error "cannot read:," then you should check whether that file is present in the directory.

    Under UNIX, check that you used the correct capitalization for

    Under Windows, use the dir shell command, not the graphical Explorer tool. Some text editors (in particular Notepad) insist on adding an extension .txt after every file. If you use Notepad to edit, then it actually saves it as Under the default Windows settings, Explorer conspires with Notepad and hides the .txt extension because it belongs to a "known file type." In that case, you need to rename the file, using the ren shell command.

  • If java reports an error message complaining about a java.lang.NoClassDefFound-Error, then carefully check the name of the offending class.

    If the interpreter complains about welcome (with a lowercase w), then you should reissue the java Welcome command with an uppercase W. As always, case matters in Java. If the interpreter complains about Welcome/java, then you accidentally typed java Reissue the command as java Welcome.

    If the interpreter complains about Welcome, then someone has set the class path on your system. You need to either remove the setting of that environment variable, or add the current directory (symbolized as a period) to the class path. See for more details.

  • If you have too many errors in your program, then all the error messages fly by very quickly. The java interpreter sends the error messages to the standard error stream which makes it a bit tricky to capture them if they fill more than one screen.

    On a UNIX or Windows NT/2000/XP system, this is not a big problem. You can use the 2> shell operator to redirect the errors to a file:

    javac 2> errors.txt

    Under Windows 95/98/ME, you cannot redirect the standard error stream from the command shell. You can download the errout program from and run

    errout javac > errors.txt

Java graphics exclamatory_icon.gif

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

Java ScreenShot