This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes main noDefClassStatement found Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "main noDefClassStatement found" Watch "main noDefClassStatement found" New topic
Author

main noDefClassStatement found

Lynn Cross
Greenhorn

Joined: Jul 25, 2004
Posts: 3
Can anyone tell me why this is not working. I've busted my little brain for days. Very new at Java, please help. This is the exact error message.

Exception in thread "main" java.lang.NoClassDefError:
import java.io.*;
public class Quiz
{
public static void main(String args[])
{
int correctCounter;
InputStreamReader reader;
BufferedReader buffer; keyboard = new
BufferedReader (new InputStreamReader (System.in));
}
String seekResponse;
System.out.println("Welcome to Hoffman School .. ");
{
// choose the subject
System.out.println("enter h for humanities, e for English or x for exit");
seekResponse = keyboard.readline();
if (seekResponse.equals ("h"); //do humanities quiz
{System.out.println("The Declaration of Independence is the same as the
constitution");
seekResponse = keyboard.readline();
if (seekResponse=("t"));
System.out.println("incorrect answer");
if (seekResponse.equals ("f"));
System.out.println("correct");
System.out.println("At the Boston Tea Party the people threw coffee overboard");
if (seekResponse.equals ("t"));
System.out.println("incorrect answer");
If (seekResponse.equals ("f"));
System.out.println("correct");
}
else if (seekResponse.equals ("e"); //do English quiz

{System.out.println("A period goes at the end of a question");
seekResponse = keyboard.readline();
if (seekResponse.equals ("t"));
System.out.println("incorrect answerz");
if (seekResponse.equals ("f"));
System.out.println("correct");
System.out.println("Most sentences begin with a capital letter");
if (seekResponse.equals ("t"));
System.out.println("incorrect answer");
If (seekResponse.equals ("f"));
System.out.println("correct");
}
else if (seekResponse.equals ("x"));
exit// do exit logic
}
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi Lynn,

Welcome to JavaRanch!

If you're typing



then yes, I'd expect you to see a message like



because the class file Quiz.class -- the executable version of the code -- won't have been created. There are several syntax errors in the above, and you have to fix them, such that the "javac Quiz.java" command reports no errors, before trying to run the program.

To reiterate: NoClassDefFoundError means "No class def(initition) found."

If the above is the exact code you're trying to compile, then the first error is that the routine main() ends abruptly with what is probably just a stray closing curly brace at line 10. After that, at line 30, there's an extra semicolon, and missing close-parenthesis. The next few "if" statements are also followed by that extra semicolon, but the compiler actually won't report it -- the code is syntactically correct, but it just won't do what you intend.

Here's my advice -- feel free to take it or leave it: slow down. Get a "Hello, World" program to work, first. Then try a very slightly longer program with a single "if" statement. Then a longer one with several "if" statements. Get each program to work and make sure you understand it. Then you can work your way up to something of this size. Even when you do, try to write it a little piece at a time: write some, compile it, test it. Then add more, compile it, test it. Then add more. Make sure you understand what's happening at every step.


[Jess in Action][AskingGoodQuestions]
Darin Niard
Ranch Hand

Joined: Jun 08, 2004
Posts: 118
You don't have a compiled class to run, and the class you posted here isn't going to compile the way it is.

  • Your main method ends too early, and the rest of your code ends up outside of it.
  • You capitalized the "i" in "if" twice.
  • Always compare Strings with the equals method, not = or ==.
  • Semicolons everywhere
  • This really needs to be written as a loop.


  • [ August 03, 2004: Message edited by: Darin Niard ]
    Lynn Cross
    Greenhorn

    Joined: Jul 25, 2004
    Posts: 3
    Thanks so much for the advice. I will heed it.
    Dirk Schreckmann
    Sheriff

    Joined: Dec 10, 2001
    Posts: 7023
    Note that to run the Quiz program defined in the source file Quiz.java, the command is java Quiz not java Quiz.java. (I'm thinking that Ernest just made a small boo-boo above.)


    [How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: main noDefClassStatement found
     
    Similar Threads
    do {} while statement illegal start of type
    Write a method that can be called, that will initialize the seating plan.
    Simple input commands
    user input
    Java operator