Getting Started Problems
If you're having trouble compiling your source code or running your application or applet, this section might help you find and fix your problem. If nothing in this section helps, refer to the documentation for the compiler or the interpreter you're using.
Some of the problems that first-time programmers experience are the result of incorrectly installed development environments. If you can't compile even a single program, dou-ble-check that you installed your development environment correctly and that your path has been updated so that the operating system can find your development environment.
You can find installation instructions for the Java 2 Software Development Kit (SDK) in the README.txt file at the top of the SDK release. You can also find these instructions on the Java 2 SDK Web site. [1]
[1] http://java.sun.com/products/index.html
Another common problem results from using a text editor that saves files in 8.3 format or with a TXT suffix. Most development tools are picky about file names. Save yourself some trouble: Avoid editors that don't give you full control over file names.
One problem that vexes beginners and experts alike results from incorrectly setting the CLASSPATH environment variable. Do not set CLASSPATH unless you are sure of what you're doing.
Compiler Problems
Can't Run the Compiler
If you can't get the compiler to run at all, it's because the operating system can't find it. You probably need to either specify the full path to the compiler or set your path environment variable so that it contains the SDK's bin directory.
Note that after installing both the SDK software and documentation, the SDK directory will have the following structure.
Figure 111. The SDK installed directory tree.
Can't Find the File
If the compiler can't find the file you're trying to compile, try these solutions.
- Make sure that the file is named exactly Class .java, where Class is the name of the class in the file you're trying to compile.
- Make sure that you're invoking the compiler from the directory in which the .java file is located.
- Make sure that you invoked the compiler rather than the interpreter. The compiler is named javac; the interpreter is named java.
Note
A source file's name must exactly match the name of the class that the file contains, including the same capitalization. Be sure that the full .java suffix follows the class name.
Changes Didn't Take Effect
If you changed your program and recompiled it but the changes didn't take effect, try these solutions.
- Make sure that the program compiled cleanly. If the program couldn't compile, the old class files might still exist.
- If your program compiled successfully, make sure that you're specifying the new class files and not a backup copy of the old files. Delete the old class files, if necessary, to avoid confusion.
- Make sure that the tool you're using hasn't cached the old class files. Usually, you can empty a cache by quitting the tool and then restarting it. If you are trying to view changes made to an applet in a browser, try clicking Shift and the Reload or Refresh button. If this doesn't work, try explicitly clearing the cache on your browser.
Syntax Errors
If you mistype part of a program, the compiler may issue a syntax error. The message usually displays the name of the file in which the error occurred, the line number on which the error was detected, a description of the error (to the best abilities of the compiler), the code on that line, and the position of the error within the code. Here's an error caused by the omission of a semicolon (;) at the end of a statement:
testing.java:14: ';' expected. System.out.println("Counted " + count + " chars.") ^ 1 error
Sometimes, the compiler can't guess your intent, so it prints one or more confusing error messages for one mistake. For example, the following code snippet omits a semicolon (;) from the line in boldface:
while (in.read() != -1) count++ System.out.println("Counted " + count + " chars.");
When processing this code, the compiler issues two error messages:
testing.java:13: Invalid type expression. count++ ^ testing.java:14: Invalid declaration. System.out.println("Counted " + count + "chars."); ^ 2 errors
The compiler issues two error messages because after it processes count++, the compiler's state indicates that it's in the middle of an expression. Without the semicolon, the compiler has no way of knowing that the statement is complete.
If you see any compiler errors, your program did not successfully compile, and the compiler did not create or update your .class file. Carefully verify the program, fix the errors, and try again.
Semantic Errors
In addition to verifying that your program is syntactically correct, the compiler checks for basic correctness. For example, it warns you each time you use a variable that has not been initialized:
testing.java:13: Variable count may not have been initialized. count++ ^ testing.java:14: Variable count may not have been initialized. System.out.println("Counted " + count + " chars."); ^ 2 errors
Again, your program did not successfully compile, and the compiler did not create a .class file. Fix the error and try again.
Interpreter Problems
Can't Find the Class
If the interpreter says that it can't find the class you just compiled, try these solutions:
- Make sure that you specified the class namenot the class file nameto the interpreter. For example, the following command doesn't work: java HelloWorldApp.class. Instead, use java HelloWorldApp. (Notice that you shouldn't add .class!)
- Unset the CLASSPATH environment variable if it's set. See the section Path Help in Appendix E (page 540) for information about CLASSPATH.
- Make sure that you're invoking the interpreter from the directory in which the .class file is located.
- Make sure that you invoked the interpreter rather than the compiler. The compiler is named javac; the interpreter is named java.
The main Method Is Not Defined
If the interpreter tells you that the main method is not defined, try these solutions.
- Make sure that the program you tried to execute is an application and not just an applet. Most applets don't have main methods, because applets are designed to be executed in browsers.
- If the program should be an application, make sure that it has a main method.
- Make sure that the program's main method is defined exactly as described in the section The main Method in Chapter 1 (page 34). For example, make sure that you specify the main method as public.