Troubleshooting

If you are having trouble with the java or javac commands, this section provides some guidance about what might be wrong. If you do not feel comfortable with the command line, you might prefer to run the examples in the Eclipse IDE. See Appendix A for an introduction to Eclipse and full instructions on running the example code.

Compilation (javac) Errors

If the javac command is not found, you may have installed the JRE (Java runtime environment) instead of the JDK. Install Java 5.0 (also known as JDK 1.5), as described in "Java Tools and Environment" at the beginning of this chapter. If you are getting syntax error messages on the HelloJava examples, it is likely that your source code has a mistake or that the source file has become corrupted. The best way to eliminate this possibility is to copy the source directly from the ZIP file on the CD or download them from the examples link at the web site for this tutorial, http://www.oracle.com/catalog/learnjava3/.

Runtime (java) Errors

Most runtime errors are related to the Java CLASSPATH environment variable. If you are getting NoClassDefFound or ClassNotFound errors when attempting to run the examples using the java command, you may have a classpath issue. By default there should be no value set in the environment variable CLASSPATH, but some untidy apps may set this value for you when you install them. To display the value of CLASSPATH under Windows, use this command:

 C:\> set classpath


Use this command for Unix:

 % echo $CLASSPATH


To clear any CLASSPATH value, use this command under Windows:

 C:\> set classpath=


Use this command for Unix:

 % unset CLASSPATH; export CLASSPATH


Java Version Problems

To check the version of Java, use the -version switch with the java command. Type the following at the command prompt and press Enter:

 C:\> java -version


This should report "java version 5.0." If it doesn't, install Java 5.0 as described in "Java Tools and Environment" at the beginning of this chapter.

The getContentPane( ) Error

What if you get an error that says Exception in thread "main" java.lang.Error: Do not use javax.swing.JFrame.add( ) use javax.swing.JFrame.getContentPane( ).add( ) instead? This error also indicates that you are using an earlier version of Java (prior to Java 5.0). Install Java 5.0.

Comments