• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Insights on Class loading needed

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I got to know that class loading problem arises because of presence of different versions of same class in different modules or use of different version of same jar in different modules.I have some doubts regarding this:-
1>Why the develpers use different versions of jar is this a mistake or if not then what purpose different versions of the same jar solves?
2> Why not there is a single class loader for all it will solve all the problem?
3>Class loaders are .class files then who loads the class loaders?
4>The class not found exception that shows up basically means the class is found but the definition of the class doesn't match with
what is searched for, may be because of different versions of the class some newly introduced methods or constants are not available in the class that is loaded. Am I right?

Thanks in advance,
Abhisek
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JVM provides the initial class loader(s), as far as I know. It's atypical to use multiple versions of the same library, but is occasionally necessary for compatibility reasons, but how and why is (at least in my experience) application-specific.

A class not found exception means the class wasn't found by the class loader attempting to load it.

A single class loader doesn't necessarily handle all use-cases--there are reasons to use customize a classloader, like for run-time byte code manipulations, loading non-Java classes, etc.
 
Dash Abhisek
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks David for your insights but I really didn't get just one thing in your explanantion and that is why different versions of jars are used can you explain a bit about the compatibility reasons you talked about.

Looking forward to your reply

Thanks
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1>Why the develpers use different versions of jar is this a mistake or if not then what purpose different versions of the same jar solves?

I don't know what do you mean.

2> Why not there is a single class loader for all it will solve all the problem?

JVM use different classloaders for different reason: 1) the different way classes are loaded (system, *.class, *.jar or from network), 2) different class loader allows different access permission to be handled (in most case), 3) allows classes access to be heiratically organized and 4) ALLOWS different version of the same class to be loaded.

3>Class loaders are .class files then who loads the class loaders?
Class loader is a class that can load other classes.

4>The class not found exception that shows up basically means the class is found but the definition of the class doesn't match with what is searched for, may be because of different versions of the class some newly introduced methods or constants are not available in the class that is loaded. Am I right?

ClassNotFoundException is thrown when the bytecode of the class is not found.


About the different versions of a class. there is simple rule:

Within a class loader, all classes must have a unique full names.

So as soon as different versions of a class is not loaded by the same Classloader, it is possible to have two of them around in a running JVM. Keep in mind that, when that situation happens, a client class can only access to a version of the class (depending on what Classloader it ask).

Hope this helps.

NawaMan
 
Dash Abhisek
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response. It helped me.I have 1 more doubt :-

suppose there are 5 class loaders (CL1 ... CL5) CL1 is the parent of CL2 and CL3 ,CL2 is parent of CL4 , CL3 is parent of CL5 now if a class is loaded by CL3 is it going to be visible to CL4 or not?
 
Nawapunth Manusitthipol
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is NO. Well, If you want, you can write a ClassLoader that delegate the class search downward but all ClassLoader come with Java does not do that.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Class loaders are .class files then who loads the class loaders?


The very first (and comparatively simple) classloader is implemented in native code, not in Java. So it can be used to load the classes that make up all the classloaders that are written in Java.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic