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 Figure 2-1 on the screen. Figure 2-1. Compiling and running Welcome.java 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 Example 2-1 we will explain how it works in the next chapter. Example 2-1 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: 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 Welcome.java. 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: Welcome.java," then you should check whether that file is present in the directory. Under UNIX, check that you used the correct capitalization for Welcome.java. 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 Welcome.java, then it actually saves it as Welcome.java.txt. 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 Welcome.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 Chapter 4 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 MyProg.java 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 http://www.horstmann.com/corejava/faq.html and run errout javac MyProg.java > errors.txt There is an excellent tutorial at http://java.sun.com/docs/books/tutorial/getStarted/cupojava/ that goes into much greater detail about the "gotchas" that beginners can run into. | |