aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hey guys Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hey guys" Watch "hey guys" New topic
Author

hey guys

Rafi Fareen
Ranch Hand

Joined: Aug 28, 2009
Posts: 75
anyone can explain me the reason why i get "NoClassDefFoundError" though i can compile the program but when i run it.
my file name is not same as any of the class names (because neither of the classes are public).
if i specify a "public" access specifier to the method "coolMethod()" of Zoo class than program run, else if i leave the "coolMethod()" with default access specifier than i get that "NoClassDefFoundError".

anyone can give me a clue what technically is happening here.

here is the code :


it compiles but when i run it i get that exception, but if i specify the "coolMethod" as public OR change the class name "Moo" to file name (make it public) than it will run properly.

regards,

Rafi
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
even if none of the classes are public you have to run the program using java and the class that contains the main method


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

I saved the following code as "Moo.java" and ran its working fine...



SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Rafi Fareen
Ranch Hand

Joined: Aug 28, 2009
Posts: 75
Minhaj kaimkhani wrote:I saved the following code as "Moo.java" and ran its working fine...



that is true, it works if you change the name of the file as "Moo.java", but
do one thing, change the name of file back to something else and instead specify an access specifier to the "coolMethod()" as below code :

execute the code, it will work perfectly .... i wanna know what exactly is happening, something so tricky regarding access of method :p . i know that if you don't declare name of the file as name of the public class than the class having the main method will be executed first.

guys just execute the first code that i have supplied in my first comment to see the exact problem :p

thanks,

note: i guess appearing for scjp one should know such low level implementations, i'm curious.
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
save the file to what name?
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
are you trying this example on netbeans or eclipse?
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
yup you get error only if you run this on notepad or editlus and runs on netbeans...maybe netbeans internally finds the main method to run this file
Rafi Fareen
Ranch Hand

Joined: Aug 28, 2009
Posts: 75
Raju Champaklal wrote:yup you get error only if you run this on notepad or editlus and runs on netbeans...maybe netbeans internally finds the main method to run this file


its ok guys ... i use command prompt for practice, i don't use IDE's.
its running now without any error.

regards,

Rafi
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

Raju Champaklal wrote:yup you get error only if you run this on notepad or editlus and runs on netbeans...maybe netbeans internally finds the main method to run this file


Hi..

I run this code in Netbeans and there's no error..
Moo.java


Sorry, perhaps my english language isn't too good.. Prepare for SCJP 6, Please God help me.. ☼
References : [Java.Boot] [JavaChamp] [JavaPrepare]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Rafi when you start a topic please UseAMeaningfulSubjectLine for your topic...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Rafi Fareen
Ranch Hand

Joined: Aug 28, 2009
Posts: 75
Ankit Garg wrote:Rafi when you start a topic please UseAMeaningfulSubjectLine for your topic...


its very confusing .... look i have his piece of code.



the above code compiles perfectly, but when i run it, i will get the exception:
Exception in Thread main "java.lang.NoClassDefFoundError" enum1

considering that i don't want to have a public class (if no public class found the class having main method will be executed first), but it doesn't work for me (i use command prompt), the only only only way that i can make it work is:

1. I declare the class as public
2. I change the file name to class name
3. compile and execute the program (it works fine)

after once i have executed the program, i will change the code back to the previous format like

1. I remove the public access specifier
2. I will change the file name from class name

and the funny point is that this time it will compile and execute properly .... it really makes me confused that i can't even explain what the actual problem is.

hope you can figure out what the problem is.

I'm curious to know that, how many ways are there that we can execute a program without having the file name same as public class name.

regards,

Rafi
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

Hi Rafi Fareen..

I've tried running this code through command prompt, and there's no error..

First.java


the file should be named First.java..
the filename of that code should be First.java same as the class which has public static void main(..) method..
because, while we try to use java command in the command prompt..
the java command will look for public static void main(..)..
Rafi Fareen
Ranch Hand

Joined: Aug 28, 2009
Posts: 75
Leonardo Carreira wrote:Hi Rafi Fareen..

I've tried running this code through command prompt, and there's no error..

First.java


the file should be named First.java..
the filename of that code should be First.java same as the class which has public static void main(..) method..
because, while we try to use java command in the command prompt..
the java command will look for public static void main(..)..


thanks for the reply, if i change the name of the file to "First.java" even i will get a perfect answer, but there is another concept that is, if you don't have a public class in your file than the class with main method will be executed first .....


regards,
Rafi Fareen
Ranch Hand

Joined: Aug 28, 2009
Posts: 75
anyways, thanks guys
looks like i will have to stick with the traditional way of executing programs (name the file name after the public class).
thanks for the comments.

Rafi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hey guys