5

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.

Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
David
  • 14,569
  • 34
  • 78
  • 107
  • 2
    What tutorial are you following? Where have you seen examples of code like this? – S.Lott Mar 02 '10 at 22:08
  • 1
    I would strongly recommend following the vendor's own "Trails Covering the Basics" http://java.sun.com/docs/books/tutorial/ – BalusC Mar 02 '10 at 22:11
  • 2
    @David: Many of the answers posted here are either wrong or misleading; please see my comments on most of them. The best answer in my opinion is josefx's answer here: http://stackoverflow.com/questions/2367185/whats-wrong-here/2367253#2367253. – Michael Myers Mar 02 '10 at 22:25
  • @mmyers: +1 for the effort of educating people. :-) – LB40 Mar 02 '10 at 22:32

7 Answers7

9

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

Robert Davis
  • 2,255
  • 2
  • 21
  • 21
  • 2
    This is the second time I've seen this statement today, and it's still wrong. *Public* classes must match the file name, but the posted code is not a public class. – Michael Myers Mar 02 '10 at 22:21
  • 1
    @mmyers:Yeah you are right posted code is not the public class but the main method must be in the public class of the program. – giri Mar 02 '10 at 23:48
  • @girinie: No, that's not true either; the class with the main method can be package-private. I'd refer you to my comment on skaffman's answer, but the answer has been deleted. – Michael Myers Mar 03 '10 at 15:23
7

javac uses the class name to generate the output not the filename. So it will generate a Helo.class classfile.
java will take a class name and call the main function in the corresponding class file, here Hello.class.

The ClassNotFoundError is thrown because javac never generated an Atemp2 classfile as there is no Atemp2 class in your source file.

josefx
  • 15,506
  • 6
  • 38
  • 63
6

Rename your Atempt2.java to Hello.java to get going, then:

javac Helo.java
java Helo

See here for more discussion and the reasoning.

Community
  • 1
  • 1
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
3

change:

class Helo

to

class Atempt2

in your source file.

A .java file that declares a class must have the file name match the declared class name.

Kylar
  • 8,876
  • 8
  • 41
  • 75
1

The filename must match the name of the public class defined in the file. In this case, you would either have to name the file "Helo.java" or renamed the class to Atempt2.

William Brendel
  • 31,712
  • 14
  • 72
  • 77
  • 1
    This is true, but there is no `public class` defined in the file, which makes it irrelevant to the immediate question. – Michael Myers Mar 02 '10 at 22:28
1

This is the very basic to start with java programming.Any program you write the name of the file must match with the public class of the program. Here in your program public class of the file is Helo so your file name must be Helo.java.Here the compiler is able to compile but JVM will search for Helo.class file to run. As there is no Helo.class file you are getting runtime Exception Exception in thread "main" java.lang.NoClassDefFoundError: Atempt2

giri
  • 26,773
  • 63
  • 143
  • 176
1

to complement josefx's answer.

The argument to the compiler (javac) is the name of the file or files to compile (as you did).

On the other side, the virtual machine (java) gets the name of the class whose main method is to be executed.

One option would be

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

Normally it is a good idea to have the file named the same way as the class. For public class this is a must (checked by compiler).

user85421
  • 28,957
  • 10
  • 64
  • 87