Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java EE class loading standard

WebSphere comes with parent last and parent first. Is this Java EE compliant? Is this supported by all application servers that are Java EE 5 compliant?

like image 893
Aravind Yarram Avatar asked Nov 16 '10 21:11

Aravind Yarram


People also ask

What is the order of class loading in Java?

Note: The ClassLoader Delegation Hierarchy Model always functions in the order Application ClassLoader->Extension ClassLoader->Bootstrap ClassLoader. The Bootstrap ClassLoader is always given the higher priority, next is Extension ClassLoader and then Application ClassLoader.

Does JVM load all classes?

It loads the system classes required to run the JVM itself. You can expect all the classes that were provided with the JDK distribution to be loaded by this class loader. (A developer can expand the set of classes that the bootstrap class loader will be able to load by using the -Xbootclasspath JVM option.)

Which class loads the class for JVM?

The bootstrap class loader finds the HashMap class and loads it into the JVM memory. The same process is followed for the DNSNameService class. But, the Bootstrap ClassLoader is not able to locate it since it's in $JAVA_HOME/lib/ext/dnsns. jar .

Is it possible to load a class by two ClassLoader?

A class is always identified using its fully qualified name (package. classname). So when a class is loaded into JVM, you have an entry as (package, classname, classloader). Therefore the same class can be loaded twice by two different ClassLoader instances.


1 Answers

I did my own research (going through the specs and few blogs) and below is what I've figured

EAR

The spec DOES NOT define or mandate how the class loaders should work within an EAR. It however defines that 

  1. there SHOULD be per thread context class loader for runtime loading of classes
  2. there MIGHT be a hierarchical class loading mechanism for resolving classes (app server vendors are free to implement whichever way they choose to)
  3. the top level class loader (WAR/EAR) MAY delegate to the low level class loaders (like Bootstrap, extension etc). This is in compliance with J2SE class loader delegation model (PARENT_FIRST in WAS)

WAR

Servlet specification defines and mandates the support of a PARENT_LAST (i.e. WAR/web-inf/classes and WAR/web-inf/lib take precedence over the libraries that come with the app server) class loading model. But this is just for WAR modules. The Servlet spec diverges from the standard J2SE delegation model of PARENT_FIRST in this case.

Reference

Spec: Servlet 2.3, Section: 9.7.2 Web Application Classloader

Spec: Java EE 5, Section: EE.6.2.4.7 Context Class Loader

App Server specifics

Interestingly, though, it appears most major app servers support some mechanism of turning off delegation to isolate the application from the app server if necessary (because of conflicts or otherwise): WebSphere - "parent-last", GlassFish - <class-loader delegate="false">, JBoss - java2ParentDelegation=false, Geronimo - <java2-delgation-model>false</java2-delegation-model>

like image 82
Aravind Yarram Avatar answered Oct 28 '22 18:10

Aravind Yarram