Monday, May 19, 2008

Class Loader

Consider i am using weblogic appln server.I specify the application jars either in the weblogic classpath or put them in the lib folder of the server.These files would be loaded on application (system) class loader.If we are calling a class file from a jsp or servlet , the class file would get loaded from the application loader, jsp or servlet is being loaded by the Web loader of WLS .
If a am calling a EJB from my class file that EJB jar has to be specified either in the classpath or lib, otherwise it wont find the class.
IN this way the EJB class loader of WLS is not being used.
I think this can be resolved if we put all the web components in a war all jsp and related classes.
Would this solve this problem or is there any other turnaround???

7 comments:

RamrajChauhan said...

First I would brief about the class loaders and their hierarchy and then about this.
The bootstrap classloader is the root of the Java classloader hierarchy.
The extensions classloader is a child of the bootstrap classloader. The extensions classloader loads any JAR files placed in the extensions (ext) directory of the JDK.
Then comes the System Classpath Loader. This loads all the classes/jars present in the classpath.
If you deploy the application on weblogic and specify something in classpath those would be loaded by System Classpath Classloader.
This is also called as Application classloader for weblogic server.

now consider the sample EAR application.
EAR
--war
--WEB-INF
--classes
--lib
--jar(all ejb jars)
--META-INF
--APP-INF
--lib

the lib in APP-INF would be loaded by Application classloader.

the servlet classes would be loaded by web class loader.
The EJB's would be loaded by EJB Class Loader.
the way classloading works is that ,before loading the class itself it would ask the parent to load it,if parent is not able to do so then the child would do it.
NOw in this case since we have packaged the JAR(ejb's) and War in a EAr weblogic server would create the heirarchy as
Application
|
EJB class loader
|
WEB class loader

This situation would be different when we have web and ejb jars not deployed as a single EAR application.

On other case when web and ejb-jars are being deployed as separate applications weblogic server would create sibling classloaders for them.

So the idea is to make it simple
create your application as

Package the servlets and JSPs in a WAR file

Package the Enterprise JavaBeans in an EJB JAR file

Package the WAR and JAR files in an EAR file

Deploy the EAR file

This is the simplest way and to avoid all complications of classpaths etc.
you'll not need to specify the ejb jars in classpath or put them into lib folder.
You'll just need to add third party libraries in lib or include them in classpath.

--your comments..

Saurabh said...

Seems to be fine...

Saurabh said...

Seems to be fine...

Tapan said...

Well explained Ramraj. Just one doubt.. Your diagram shows that web class loader is the child of ejb class loader. I think it lies at the same level as ejb class loader and system class loader is parent of both. Please comment.

RamrajChauhan said...

Tapan,
That would be the case when we have jar and war deployed as seperate applications.There they would behave as siblings.
BUt in case where we have single EAR with jar and war in it,I think the detail shown in figure would be valid.

--Rgds
Ramraj

Saurabh said...

If the jar and war are deployed seperately, I think in that case the war must have the home and remote interface if they want to call a EJB from jar..am i right??

RamrajChauhan said...

yeah..you are right.

Rgds
Ramraj