Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please solve this....

 
sarah mann
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48953
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
one more thing, make your class public .



Hope this helps
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amit Ghorpade:
...one more thing, make your class public...

Why?
 
sarah mann
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all you guys... i have got rid of that nasty thing now...thanks again
 
Campbell Ritchie
Sheriff
Pie
Posts: 48953
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic