• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

ExceptionInInitializerError and NoClassDefFoundError

 
Ranch Hand
Posts: 49
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 80124
416
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it means there is some error in a Class<?> object.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
What are you doing? You are supposed to be reading this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic