• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ExceptionInInitializerError and NoClassDefFoundError

 
Kamil Hlubek
Ranch Hand
Posts: 49
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone describe the ExceptionInIntializerError and the NoClassDefFoundError, please , because I just has been reading about the NoClassDefFound exception and that it is named terribly and it is not explained in the API good enough ( Here is the source ).
I would be also happy if you could explain me when this two exceptions may occur.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamil Hlubek wrote:Could someone describe the ExceptionInIntializerError and the NoClassDefFoundError

I think the ExceptionInInitializerError is described clearly in the javadoc. Here's a snippet of the javadoc of the ExceptionInInitializerError class
Java 7 API Specification, ExceptionInInitializerError wrote:Signals that an unexpected exception has occurred in a static initializer. An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable.
So let's illustrate with an exampleOutput:
Exception in thread "main" java.lang.ExceptionInInitializerError
at TestExceptionInInitializerError.main(TestExceptionInInitializerError.java:3)
Caused by: java.lang.ArithmeticException: / by zero
at X.<clinit>(TestExceptionInInitializerError.java:7)
... 1 more


Now the NoClassDefFoundError. I agree the description in its javadoc is a little bit harder to understand. Here's a snippet of the javadoc of the NoClassDefFoundError class
Java 7 API Specification, NoClassDefFoundError wrote: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.
So when a class could not be found, this error will be thrown.

Now it's time for another example. Assume these two (very simple) classes, each in its own source code fileNow let's compile these classesBoth will successfully compile and now you can execute the TestHello classOutput (as expected): Hello world!

If you now simply delete the Hello.class file and execute the TestHello class againyou'll get this output
Exception in thread "main" java.lang.NoClassDefFoundError: Hello
at TestHello.main(TestHello.java:3)
Caused by: java.lang.ClassNotFoundException: Hello
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more


Hope it helps!
Kind regards,
Roel
 
Kamil Hlubek
Ranch Hand
Posts: 49
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, but what is meaning the fifth number in http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4.2?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamil Hlubek wrote:but what is meaning the fifth number in http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4.2?

Don't know, don't care! You don't need to know this for the OCA (or OCP) exam. And I'm a professional Java developer for 10 years and it's the very first time I see this JLS section. Enough said, isn't it?

But just for fun, I put together a (crazy looking) programIf you run that program, you'll get the following output:So because of the division by zero (line1), the initialization of class X failed, so class X is in the erroneous state. According to the above application, it seems you can still request some information about this class (e.g. list all fields), but if you do fun stuff (create an instance, print the value of a field,...) you'll get a NoClassDefFoundError telling you the class was not initialized.

Kind regards,
Roel

PS. This is waaaaaaaaaaaaaay (and I could have added a few more a's if I wanted to ) beyond what you need to know for the OCA (and OCP) exams. I used reflection (to get the declared fields) which is also not on the OCA/OCP exams and was only used for demonstration purpose only.
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it means there is some error in a Class<?> object.
 
Ankit Garg
Sheriff
Posts: 9577
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:I put together a (crazy looking) program

It's not "crazy looking", it's crazy period
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic