This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hey guys

 
Rafi Fareen
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even if none of the classes are public you have to run the program using java and the class that contains the main method
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I saved the following code as "Moo.java" and ran its working fine...

 
Rafi Fareen
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
save the file to what name?
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are you trying this example on netbeans or eclipse?
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 489
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rafi when you start a topic please UseAMeaningfulSubjectLine for your topic...
 
Rafi Fareen
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 489
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic