Common Error Messages on Microsoft Windows SystemsCommon Error Messages on UNIX Systems
'javac' is not recognized as an internal or external command, operable program or batch file
If you receive this error, Windows cannot find the compiler (
javac
).Here's one way to tell Windows where to find
javac
. Suppose you installed the JDK inC:\jdk6
. At the prompt you would type the following command and press Enter:If you choose this option, you'll have to precede yourC:\jdk6\bin\javac HelloWorldApp.javajavac
andjava
commands withC:\jdk6\bin\
each time you compile or run a program. To avoid this extra typing, consult the section Update the PATH variable in the JDK 6 installation instructions.
javac: Command not found
If you receive this error, UNIX cannot find the compiler,
javac
.Here's one way to tell UNIX where to find
javac
. Suppose you installed the JDK in/usr/local/jdk6
. At the prompt you would type the following command and press Return:Note: If you choose this option, each time you compile or run a program, you'll have to precede your/usr/local/jdk6/javac HelloWorldApp.javajavac
andjava
commands with/usr/local/jdk6/
. To avoid this extra typing, you could add this information to your PATH variable. The steps for doing so will vary depending on which shell you are currently running.Syntax Errors (All Platforms)
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 (Semantic Errors;
) 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.
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.
Error Messages on Microsoft Windows Systems
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp
If you receive this error,
java
cannot find your bytecode file,HelloWorldApp.class
.One of the places
java
tries to find your.class
file is your current directory. So if your.class
file is inC:\java
, you should change your current directory to that. To change your directory, type the following command at the prompt and press Enter:cd c:\javaThe prompt should change to
C:\java>
. If you enterdir
at the prompt, you should see your.java
and.class
files. Now enterjava HelloWorldApp
again.If you still have problems, you might have to change your CLASSPATH variable. To see if this is necessary, try clobbering the classpath with the following command.
set CLASSPATH=Now enter
java HelloWorldApp
again. If the program works now, you'll have to change your CLASSPATH variable. To set this variable, consult the Update the PATH variable section in the JDK 6 installation instructions. The CLASSPATH variable is set in the same manner.Error Messages on UNIX Systems
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp
If you receive this error,
java
cannot find your bytecode file,HelloWorldApp.class
.One of the places
java
tries to find your bytecode file is your current directory. So, for example, if your bytecode file is in/home/jdoe/java
, you should change your current directory to that. To change your directory, type the following command at the prompt and press Return:cd /home/jdoe/javaIf you enter
pwd
at the prompt, you should see/home/jdoe/java
. If you enterls
at the prompt, you should see your.java
and.class
files. Now enterjava HelloWorldApp
again.If you still have problems, you might have to change your CLASSPATH environment variable. To see if this is necessary, try clobbering the classpath with the following command.
unset CLASSPATHNow enter
java HelloWorldApp
again. If the program works now, you'll have to change your CLASSPATH variable in the same manner as the PATH variable above.
A 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.Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp/classThe
main
Method Is Not Defined
The Java Virtual Machine requires that the class you execute with it have amain
method at which to begin execution of your application. A Closer Look at the "Hello World!" Application discusses themain
method in detail. If you are missing this method, you'll see the following error at runtime:
In the above message,Exception in thread "main" java.lang.NoSuchMethodError: mainclassname
is the name of the class that you tried to run.