GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes please solve this.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "please solve this...." Watch "please solve this...." New topic
Author

please solve this....

sarah mann
Greenhorn

Joined: Jul 02, 2007
Posts: 2
i just installed jdk1.5.0 n wrote this sample code

class Ex{
public static void main( String args[ ])
{ System.out.println("welcome");

}
}

this code is compiling without any error but when i m executing it it give the folloing message :
exception in thread "main" java.lang.NoClassDefFoundError

Is there any istallation problem. if yes please please tell me the the right way to do it .
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
Welcome to the Ranch.

Please alter your log-on name to match the Ranch's naming policy.
Please write words in full "and" not "n".
Please put all quoted code inside CODE tags, because it makes it easier to read.

I presume you got that error message in response to writing

javac Ex.java<enter>
java Ex<enter>.

For the example you quote that is what you ought to write. It also presume it actually said
. . . NoClassDefFoundError Ex
because you must type the class name exactly the way it is spelt in the Java code, without a . or extension.

I don't think you have an installation problem; that usually produces an error message like "java is not recognised . . .". Please check your classpath that it contains a dot; on Windows it would include ;.; and on Linux/Mac/Unix :.: also making sure there are no extraneous spaces eg ; .; or :. :
I don't think it matters if the same thing appears twice in the classpath.

This is what it says about NoClassDefFoundError in the API
Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.
You haven't managed to move or delete the .class file, have you?
Explore the folder you happen to be in at the present, and make sure it contains files called Ex.java and Ex.class (or use "ls" in Linux, or "dir" on Windows which should show the folder's contents).

You will find details about setting up a very first program here in the Java Tutorials. Go through it, and look through the "common problems" bit. I think you have your PATH set up correctly already, but you can see how to set it in the installation instructions, here for Windows, it tells you how to set the PATH variable.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. "Handles" aren't acceptable. You can change your display name here. Thanks!

As to your question: this is a very common problem. You, or some other software, has set the CLASSPATH environment variable (which tells Java where to look for class files) so that it doesn't include the current directory. You need an entry of just "." (a period) to stand for this. You might find this helpful.


[Jess in Action][AskingGoodQuestions]
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Hi,
one more thing, make your class public .



Hope this helps


SCJP, SCWCD.
|Asking Good Questions|
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Amit Ghorpade:
...one more thing, make your class public...

Why?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
sarah mann
Greenhorn

Joined: Jul 02, 2007
Posts: 2
thanks all you guys... i have got rid of that nasty thing now...thanks again
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
You're welcome.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Hi,
I am aware that Marc is one of the most talented heads out there and
this question means i went wrong somewhere.
but i wanted to say that it is just a convention to have the class with the main method to be declared public.
No access modifier means the class has a default access.And in a Java file only on class can be public but there can be n number of non-public classes.
As far as i know..

Please correct me if i am wrong
Thanks in advance
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Amit Ghorpade:
...it is just a convention to have the class with the main method to be declared public. No access modifier means the class has a default access...

Yes, it might be a good idea to make the main class public. But default access would not cause the problems that the original poster was having. So there's nothing "wrong" with the suggestion -- I just thought it needed some clarification about why "public" makes sense (which you provided above).
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Hi,
Thank you Marc,
with people like you around, I am damn sure that there is no room for questions at all only answers and answers everywhere..

Hats off to people like you..

Wish we have you all the way and all the time..

Preparing for SCJP1.5

Thank you again
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Amit Ghorpade:
...Marc is one of the most talented heads out there...

Well, that's just wrong. But thanks!
[ July 05, 2007: Message edited by: marc weber ]
 
GeeCON Prague 2014
 
subject: please solve this....