1. Home
  2. Computing & Technology
  3. Java
Paul Leahy
Paul's Java Blog

By Paul Leahy, About.com Guide to Java

Answer to Monday’s Question

Sunday September 14, 2008

This week’s question centered around this code:

// 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!");

   }
}
that had been saved in a file called JollyMessage.java.

As I see from Monday's comments, the eagle-eyed of you quickly noticed that the class is called “Jollymessage” whereas the filename is “JollyMessage.java”.

For those that didn't see the error, remember that 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. So, when you come to run the program called JollyMessage, you can't because there is no file called JollyMessage.class.

The error you get when you try and run a program with the wrong name is:

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

Happily it’s a simple fix, just change the class name so that it matches the file name (or vice versa).

Comments
September 15, 2008 at 4:13 pm
(1) Louie Cervantes says:

Students make this sort of mistake all the time. It is particularly likely if they are learning Java as a second language. The habits gained while using another language often interfere with the early lessons in Java. Good job for highlighting this important detail. A lot of beginners can avoid the usual head scratching that results from not knowing this beforehand.

September 16, 2008 at 2:45 am
(2) Lennie says:

Hi Louie: Your correct, if a student coded in VB for example thats not case sensitive then they will make this sort of mistakes alot. Those that use C-style languages (Java, C#, C, C++) are all case sensitive.

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Explore Java
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Java

©2010 About.com, a part of The New York Times Company.

All rights reserved.