"Exception in thread" main "java.lang.NoClassDefFoundError" when starting a java program from the command line

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 enter the terminal, I do:

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

and then he gives me this error message:

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

So, 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 to this, so please explain things very simply.

Thank.

+5
source share
7 answers

Rename Atempt2.javato Hello.javato go, and then:

javac Helo.java
java Helo

See here for a more detailed discussion and discussion .

+6
source

, java, , .

+9

javac , . , Helo.class.
java , Hello.class.

ClassNotFoundError, javac Atemp2, Atemp2. ​​

+7

:

class Helo

to

class Atempt2

.

.java, , , .

+3

public class, . "Helo.java", Atempt2.

+1

Java-. , , . - Helo, Helo.java. , JVM Helo.class . Helo.class, Exception Exception Exception "main" java.lang.NoClassDefFoundError: Atempt2

+1

josefx.

(javac) - ( ).

, (java) , main .

One of the options:

javac Atempt2.java    // the file name
java Helo             // the class name

It is generally recommended that you have the file in the same way as the class. For public classthis is a prerequisite (checked by the compiler).

+1
source

All Articles