Common Java Runtime Errors

Desktop Pc In Darkroom
Serkan Ismail / EyeEm / Getty Images

Consider the following segment of Java code, stored in a file called JollyMessage.java:

 // A jolly message is written to the screen!
class Jollymessage
{
   public static void main(String[] args) {
     //Write the message to the terminal window
     System.out.println("Ho Ho Ho!");
   }
}

At program execution, this code will produce a runtime error message. In other words, a mistake has been made somewhere, but the error won’t be identified when the program is compiled, only when it is run.

Debugging

In the example above, notice that the class is called “Jollymessage” whereas the filename is called JollyMessage.java.

Java is case sensitive. The compiler won’t complain because technically there is nothing wrong with the code. It will create a class file that matches the class name exactly (i.e., Jollymessage.class). When you run the program called JollyMessage, you'll receive an error message because there is no file called JollyMessage.class.

The error you receive when you run a program with the wrong name is:

Exception in thread “main” java.lang.NoClassDefFoundError: JollyMessage (wrong name: JollyMessage).. 

Common Runtime-Error Solutions

If your program compiles successfully but fails at execution, review your code for common mistakes:

  • Mismatched single and double quotes
  • Missing quotes for strings
  • Incorrect comparison operators (e.g., not using double equal signs to indicate assignment)
  • Referencing objects that don't exist, or don't exist using the capitalization supplied in the code
  • Referencing an object that has no properties

Working within integrated development environments like Eclipse can help you avoid "typo"-style errors.

To debug productionalized Java programs, run your Web browser's debugger—you should see a hexadecimal error message that can assist in isolating the specific cause of the problem.

In some situations, the problem may lie not in your code, but in your Java Virtual Machine. If the JVM is choking, it may kick out a runtime error despite the lack of a deficiency in the program's codebase. A browser debugger message will help isolate code-caused from JVM-caused errors.