Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Exception in thread "main" java.lang.NoClassDefFoundError' when running java program from command line

Tags:

java

What am I doing wrong here:

class Helo { 
   // main: generate some simple output 
   public static void main (String[] args) { 
      System.out.println ("Hello, world."); // print one line 
      System.out.println ("How are you?"); // print another 
   } 
} 

When I go into terminal I do:

cd ~
javac Atempt2.java (//that's the file name) 
java Atempt2 

and then it gives me this error message:

Exception in thread "main" java.lang.NoClassDefFoundError: Atempt2

So all in all this is what I do and what happens:

david-allenders-macbook-pro:~ davidallender$ cd ~
david-allenders-macbook-pro:~ davidallender$ javac Atempt2.java
david-allenders-macbook-pro:~ davidallender$ java Atempt2
Exception in thread "main" java.lang.NoClassDefFoundError: Atempt2
david-allenders-macbook-pro:~ davidallender$ 

I am very new at this so please explain things in a very simple manner.

Thanks.

like image 295
David Avatar asked Mar 02 '10 22:03

David


People also ask

How do I fix Java Lang NoClassDefFoundError error?

You can fix NoClassDefFoundError error by checking following: Check the exception stack trace to know exactly which class throw the error and which is the class not found by java.

What is exception in thread main Java Lang NoClassDefFoundError?

NoClassDefFoundError is a common error in Java that occurs if a ClassLoader cannot find a particular class in the classpath while trying to load it. The Exception in thread "main" suggests that this error has occurred in the main thread, the thread which is responsible for running the Java application.

Can we handle NoClassDefFoundError?

It is thrown by the Java Runtime System when the class definition is not found at run time. Developers can handle an exception using a try and catch block or any other method for preventing the program from crashing. The program will crash whenever NoClassDefFoundError will occur.


1 Answers

Its been awhile since I've done any java work but I'm pretty sure your class name needs to match your file name.

like image 85
Robert Davis Avatar answered Nov 15 '22 12:11

Robert Davis