Sunday, February 10, 2008

Common Problems in running and compiling a Java Program (and their Solutions)

1. Compiler Errors

'javac' is not recognized as an internal or external command, operable program or batch file


This is the most common of all the errors faced by people trying to compile their first piece of code. This message means that Windows cannot find the compiler (javac).

Here's one way to tell Windows where to find javac. At the prompt type the following command and press Enter:

C:> "\Program Files\Java\jdk1.6.0_\bin\javac HelloWorldApp.java

If you choose this option, you'll have to precede your javac and java commands with C:> "\Program Files\Java\jdk1.6.0_\bin\ each time you compile or run a program. To avoid this extra typing and to be able to conveniently run the JDK executables (javac.exe, java.exe, javadoc.exe, etc.) from any directory without having to type the full path of the command, you should update the PATH variable.

To set the PATH permanently, add the full path of the jdk1.6.0_\bin directory to the PATH variable. Typically this full path looks something like C:\Program Files\Java\jdk1.6.0_\bin. Set the PATH as follows on Microsoft Windows:

1. Click Start > Control Panel > System on Windows XP or Start> Settings > Control Panel > System on Windows 2000.

2. Click Advanced > Environment Variables.

3. Add the location of bin folder of JDK installation for PATH in User Variables and System Variables. A typical value for PATH is:

C:\Program Files\Java\jdk1.6.0_\bin

The PATH environment variable is a series of directories separated by semi-colons (;) and is not case sensitive. Microsoft Windows looks for programs in the PATH directories in order, from left to right. You should only have one bin directory for a JDK in the path at a time. Those following the first instance are ignored. If one is already present, update it to jdk1.6.0_\bin. If you are not sure where to add the path, add it to the right end of the PATH in the User Variables. The new path takes effect in each new command window you open after setting the PATH variable.

2. 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:

testing.java:14: `;' expected.
System.out.println("Input has " + count + " chars.")
^
1 error

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.

3. 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:

testing.java:13: Variable count may not have been initialized.
count++
^

Again, your program did not successfully compile, and the compiler did not create a .class file. Fix the error and try again.

4. Runtime Errors

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 in C:\uiet, you should change your current directory to that. To change your directory, type the following command at the prompt and press Enter:

cd C:\uiet

The prompt should change to C:\uiet>. If you enter dir at the prompt, you should see your .java and .class files. Now enter java HelloWorldApp again.


Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp/class

A common mistake made by beginner programmers is to try and run the java launcher on the .class file that was created by the compiler. For example, you'll get this error if you try to run your program with java HelloWorldApp.class instead of java HelloWorldApp. Remember, the argument is the name of the class that you want to use, not the filename.


Exception in thread "main" java.lang.NoSuchMethodError: main

The Java VM requires that the class you execute with it have a main method at which to begin execution of your application.

I hope all the errors that have been bugging you fall in one of the above categories. Still, if you are confronted with an error that is not listed above do write to me.

No comments:

 
Page Views: