wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why Noclassdeffounderror Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why Noclassdeffounderror " Watch "why Noclassdeffounderror " New topic
Author

why Noclassdeffounderror

pawni jain
Greenhorn

Joined: Apr 10, 2008
Posts: 22
hello ranchers

I wrote a code ,in that an exception arises at runtime that is NoclassdefFound error
but rule is that if i did not declare any class as public then i can give any source file name so why the following code gives error.

package javaapplication1;

interface F{}
class A implements F{}
class B extends A{}
class C extends B{
public static void main(String args[])
{
B x=new B();
F f=(C)x; //line1
}
}

and also tell me if line1 is valid ..

Thanks,
Regards.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Paste the exact error your are getting ?


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
pawni jain
Greenhorn

Joined: Apr 10, 2008
Posts: 22
hello sunny

exact error is as follows..

java.lang.NoClassDefFoundError: javaapplication1/test1
Exception in thread "main"
Java Result: 1

where javaapplication1/test1 : package/class

thanks,
Regards
Jack Crifer
Greenhorn

Joined: May 18, 2008
Posts: 29
I think C is a sub class of B , B cann't pass instanceof C test .so B cannot be cast to C
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

look at the following points:

java.exe command call following

C.main( args[] - you supplied at rum time )

Conditions:

1) main method is being called directly, without creating an instance, only static method can be called without creating an instance, so main should be static.

2) We are calling main method from outside the enclosing class (class C),
So it should be public, because only public method can be called from outside the class and package.

3) java.exe should have access to enclosing class.

i think your code, did not fulfill the third condition, so do something so that java.exe can access your class which is inside your package.( this is your homework )

I hope this will work and i do not guarantee this solution, this is as per my understanding.
[ May 20, 2008: Message edited by: Sunny Jain ]
pawni jain
Greenhorn

Joined: Apr 10, 2008
Posts: 22
@ sunny

I did not get you.you are saying

java.exe should have access to enclosing class.

what is this...

My doubt is only this ..

I have created a source file without declaring a public class in it.
so i can give any name to this source file..i did that and in that run time exception occur ..why so
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

First of all, if you are putting your class file anywhere apart from bin folder and if there is no public class in the source file, you will get this error.

you can choose different file name of your source file. there is no hard and fast rule but it is very very very BAD practice. suppose if you make one source file with a class Hello in it.
you choose a different name like:

JAVA.java

you can compile this source file :

javac JAVA.java

but after the compilation, the .class file generated will be having same name as that of class inside JAVA.java
so as per this example class file be

javac JAVA.java --> Hello.class

so in order to run it:

java Hello

Now if you Hello.class is in the same folder as that of java.exe , then i think default class trial will work, but if they are in different folder then i dont think it will work , but you can try this..!
sridhar row
Ranch Hand

Joined: Jan 16, 2008
Posts: 162
Your code will give a ClassCastException in line 1 because you are downcasting B to C and B 'IS-NOT-A' C.

If you want to run your program
1)create a folder by name javaapplication1...example: c:\javaExamples\javaapplication1

2)put your source file in it and name it test1.java

3) compile it using javac javaapplication1/test1.java from your current dir that is javaExamples.

4) run it as java javaapplication1.C it will give you the above exception
pawni jain
Greenhorn

Joined: Apr 10, 2008
Posts: 22
@ sunny and sridhar
Thanks..

sridhar as you told it gives classcastexception due to downcasting..

but take another example in case of iterator method it returns object type and we have to cast into required type(collection type) that is also downcasting and valid one..

If i am not correct hen please correct me.

Thanks,
regards.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Pawni, Here you need to understand the concept
we can do like that :

Object obj = new String();
String str = (String) obj;

This will compile and run fine, because in reality obj refer to a String Object so we can again retrieve that String Object form obj. This is the reason why compiler does not complain about this.

What you were trying in your code :

Object obj = new Object();
String str = (String)obj;

Compiler again pass this because obj can refer to String object also, to which Object obj is referring is unknown to compiler at compilation time. So compiler has to pass this although it is wrong, At the run time you can not retrieve a String from an Object's object, so you get ClassCastException.

I would recommend you to read 2nd Chapter of K&B. It will clear your concept very well.
pawni jain
Greenhorn

Joined: Apr 10, 2008
Posts: 22
@sunny

Thanks a lot
i got the concept.


Regards.
sherin stephen
Greenhorn

Joined: May 14, 2006
Posts: 6

i think you are trying to run it with
> java test1
which is the name of your source file.

> java C
is the correct usage here , because there is no "class" like test1. its just the source file name.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why Noclassdeffounderror
 
Similar Threads
voodoo exam_1 question
voodoo exam_1 question
Array Assignments
Hierarchy in Exceptions