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.

Platform Specific Details Setting the Path

UNIX:

If you use the C shell (csh), you can set the path by adding the following line to your startup file (~/.cshrc):

set path=($path /usr/local/JDK1.3/bin)

Then load the startup file and use the which command to verify that the path is set correctly:

% source ~/.cshrc % which javac

Win32:

Open the C:AUTOEXEC.BAT file and edit the PATH statement with the system editor. (Open the system editor from the Start menu, select Run, and type sysedit in the textbox.) Ensure that no other versions of the SDK are in the path, and then add the SDK to the end of the path. Here's an example of a PATH statement:

PATH C:WINDOWS;C:WINDOWSCOMMAND;C:JDK1.3BIN

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.

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.

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:

The main Method Is Not Defined

If the interpreter tells you that the main method is not defined, try these solutions.

Категории