How do I fix a java.lang.NoClassDefFoundError in my Java program?

  • Context: Comp Sci 
  • Thread starter Thread starter clook
  • Start date Start date
  • Tags Tags
    Java Program
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 3K views
clook
Messages
32
Reaction score
0
Supposed to make a program that displays an entered name and date, etc, but it won't run.

Code:
import java.util.*;
import java.text.*;
import javax.swing.*;

public class ch2NameAndDate {


	public static void main(String[] args) {
  		// Input the first, middle, and last name
		// Display the name and the date.
		String firstNameString, middleNameString, lastNameString;
		
		//Create a Scanner object
		Scanner inputScanner = new Scanner(System.in);
		
		//Objects needed for date.
		Date todayDate = new Date();
		SimpleDateFormat formattingSimpleDateFormat=
			new SimpleDateFormat("MMMM dd yyyy");
		
		//Input the data
		System.out.print("enter your First name  ");
		firstNameString = inputScanner.next();
		//firstNameString = JOptionPane.showInputDialog("Enter your First Name");
		
		//middleNameString enter middle name.
		System.out.print("enter your Middle name  ");
		middleNameString = inputScanner.next();

		
		//lastNameString = JOptionPane.showInputDialog("enter your Last name");
		System.out.print("Enter your Last name ");
		lastNameString = inputScanner.next();
		
		//Print out first, middle, and last name with date.  
		System.out.println("Hello" + lastNameString + "," + " " + firstNameString + " " + str.substring(0); + "." + "today is " + 
		formattingSimpleDateFormat.format(todayDate));
	}

}

I tried running it and eclipse said:
Code:
java.lang.NoClassDefFoundError: NameAndDate
Exception in thread "main"

what's wrong? I thought I declared the class in the code.
 
Physics news on Phys.org
Let me guess -- you saved the file as NameAndDate.java? If so, then it expects to find a class named NameAndDate in the file... and that's not the name of the class you defined.
 
Ahh, thanks. That fixed the problem.