Start of Tutorial > Start of Trail |
Search
Feedback Form |
If you're having trouble compiling or running your application, this section might be able to help. If nothing in this section helps, please refer to the release documentation for the JDK that you are using.
Can't Locate the CompilerOn UNIX systems, you may see the following error message if your path isn't set properly:
The "first steps" section at the beginning of this trail explains how to modify yourjavac: Command not foundPATH
environment variable to include the directory where the compiler lives.Syntax Errors
If you mistype part of a program, the compiler may issue a syntax error. The message usually displays the type of the error, the line number where the error was detected, the code on that line, and the position of the error within the code. Here's an error caused by omitting a semicolon (
;
) at the end of a statement:Sometimes the compiler can't guess your intent and prints a confusing error message or multiple error messages if the error cascades over several lines. For example, the following code snippet omits a semicolon (testing.java:14: `;' expected. System.out.println("Input has " + count + " chars.") ^ 1 error;
) from the bold line:When processing this code, the compiler issues two error messages:while (System.in.read() != -1) count++ System.out.println("Input has " + count + " chars.");The compiler issues two error messages because after it processestesting.java:13: Invalid type expression. count++ ^ testing.java:14: Invalid declaration. System.out.println("Input has " + count + " chars."); ^ 2 errorscount++
, 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, then your program did not successfully compile, and the compiler did not create a
.class
file. Carefully verify the program, fix any errors that you detect, and try again.Semantic Errors
In addition to verifying that your program is syntactically correct, the compiler checks for other basic correctness. For example, the compiler warns you each time you use a variable that has not been initialized:
Again, your program did not successfully compile, and the compiler did not create atesting.java:13: Variable count may not have been initialized. count++ ^ testing.java:14: Variable count may not have been initialized. System.out.println("Input has " + count + " chars."); ^ 2 errors.class
file. Fix the error and try again.
Can't Find ClassA common error of beginner programmers is to try and run the Java launcher on the
.class
file that was created by the compiler. For example, if you try to run your program withjava HelloWorldApp.class
instead ofjava HelloWorldApp
, you'll see this error message:The argument is the name of the class that you want to use, not the filename.Can't find class HelloWorldApp.classThe
main
Method Is Not DefinedThe Java Virtual Machine requires that the class you execute with it have a
main
method at which to begin execution of your application. The main Method discusses themain
method in detail. If you are missing this method, you'll see the following error at runtime:
In the above message,In class classname: void main(String argv[]) is not definedclassname
is the name of the class that you tried to run.Changes to My Program Didn't Take Effect
Sometimes when you are in the edit/debug/run cycle, it appears that your changes to an application didn't take effect -- a print statement isn't printing, for example. This is common when running applications on MacOS using Java Runner. If you recompile a
.class
file, you must quit Java Runner and bring it up again, since Java Runner does not reload classes.
Start of Tutorial > Start of Trail |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.