Fixing Common Java Errors

Whether you are just starting your first Java application, or you are a well experienced programmer, your code may not be a flawless code and compile successfully from the first time. At least you may confuse variable names, forget to declare a variable, or miss a semicolon.

Java error, like all other programming languages may be either syntax errors or logic errors. Syntax error, also called the compilation error, illegal use of statements in the form of programming language rules. When the compiler catches them it generates an error message indicating the class name, line number where the error is found, and the type of error. Sometimes the actual error may not actually be in the line indicated in the message. They want to look at and somewhere before these lines to find fault.

This happens because syntax errors can be difficult sometimes, an error can lead to more than one error. A missing semicolon e.g. sheep following one or more lines to be invalid, and thus generates error messages for all of them. When you correct this error, all subsequent messages will be cleared.

When you write a program with Java, syntax errors are almost inevitable, so do not panic or be disappointed when you encounter them. Some of these errors are very common and you are very likely to encounter one or more of them when compiling your code. The fact is you can not avoid them. So the best way to treat them is to get yourself acquainted with them, and know how to solve them. Below are some of the most common errors with their solutions.

Capitalization Errors: Java language is case-sensitive, variable names must be exactly the same, everything is not the same as total and not the same as total.

The filename is different than the public class name: The class must be saved in a file with exactly the same name, a class shirt to be saved in a file named Shirt.java. Save it for another filename will provide a collection errors.

Javac can not find the class: When you save your classes, or source code, they must be in the same folder where your javac.exe is. This file is responsible for compiling your codes and set up automatically when you download and install the DSK machine. Generally you want to install the SDK to C: drive so there where your javac file will reside. So when you save your source code you should save it in the C: folder in a folder named Java.

"javac is not recognized as an internal or external command, operable program or batch file" (Windows System) or "javac: Command not found" (for UNIX System): This means that your compiler can not find your javac. This is due to one of two reasons, either you do not have a javac at all, because you have not installed the right machine, or you have not set your PATH correctly. In this case you properly set your PATH, or you'll have to write the full file name path to execute it.

"Exception in thread" main "java.lang.NoClassFoundError: 'filename': You probably misspelled the filename, remember that Java is case-sensitive.

"Line nn: ';' expected": This means that you moved to a new line without signaling that the compiler. When your string, or line of code is too long to fit on one line, you may split into two lines. To do it just by pressing the Enter key will confuse the compiler and generate an error. Instead, you must link with a '+' sign. It is split your string into two lines, ending the first with a '+' before he moved to the second line.