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

  • Thread starter Thread starter clook
  • Start date Start date
  • Tags Tags
    Java Program
AI Thread Summary
The discussion centers on resolving a java.lang.NoClassDefFoundError encountered while running a Java program intended to display a name and date. The error arises because the class name in the code, "ch2NameAndDate," does not match the filename, which was mistakenly saved as "NameAndDate.java." A participant clarifies that Java requires the filename to correspond to the defined class name, leading to the resolution of the issue. After renaming the file to match the class name, the program runs successfully. This highlights the importance of consistent naming conventions in Java programming.
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.
 

Similar threads

Back
Top