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.

4
  • 2
    What tutorial are you following? Where have you seen examples of code like this? Commented Mar 2, 2010 at 22:08
  • 1
    I would strongly recommend following the vendor's own "Trails Covering the Basics" java.sun.com/docs/books/tutorial Commented Mar 2, 2010 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: stackoverflow.com/questions/2367185/whats-wrong-here/…. Commented Mar 2, 2010 at 22:25
  • @mmyers: +1 for the effort of educating people. :-) Commented Mar 2, 2010 at 22:32

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
@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.
@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.
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.

1 Comment

+1: This is the only correct answer posted so far that doesn't also mislead the OP.
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.

2 Comments

or name the class within the file Atempt2
Renaming will solve the problem, but it doesn't explain what is going on right now.
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.

1 Comment

Not quite correct; please see my comment on Robert Davis's answer.
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.

1 Comment

This is true, but there is no public class defined in the file, which makes it irrelevant to the immediate question.
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

Comments

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.