wood burning stoves 2.0*
The moose likes Servlets and the fly likes Order in which class is searched Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Order in which class is searched" Watch "Order in which class is searched" New topic
Author

Order in which class is searched

A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

What is the order in which a class is searched...in a web application deployed

like say...lib folder,classes folder,jdk ext folder..etc..

Tx in advance,
Regards
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Seems to me that this is the type of question that would be answered by looking at the Servlets Specification. (Hint: chapter 9)

Originally posted by A Kumar:
Tx in advance,


Texas in advance?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

P.S. The Servlet Spec can't spell out the whole story since there are container-specific nuances as well. So you'll need to check out the docs for your container as well.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Usually this is the order
1. Bootstrap classes like java.lang.String
2. Classes in java.ext.dirs
3. System classpath defined by CLASSPATH
4. AppServer libraries inside of some lib folder
5. For web app - lib folder first and then the classes folder.


Groovy
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
It depends on your application server class loader hierachy.

Also the delegation mode(delegating it to parent class loader) which you can turn on and off.

J2EE Spec should have a standard class loading hierachy as mentioned by Prabhat, but application server vendors may have a more specialized implementation for example JBoss application server uses a UnifiedClassLoader.


Excerpt from Java/J2EE Job Interview Companiondiagram is missing as i could not paste it.




The J2EE application specific class loaders are children of the �System �classpath� class loader. When the parent class loader is above the �System �Classpath� class loader in the hierarchy as shown in the diagram (i.e. bootstrap class loader or extensions class loader) then child class loaders implicitly have visibility to the classes loaded by its parents. When a parent class loader is below a �System -Classpath� class loader then the child class loaders will only have visibility into the classes loaded by its parents only if they are explicitly specified in a manifest file (MANIFEST.MF) of the child class loader.

Example As per the diagram, if the EJB module MyAppsEJB.jar wants to refer to MyAppsCommon.jar and MyAppsUtil.jar we need to add the following entry in the MyAppsEJB.jar�s manifest file MANIFEST.MF.

class-path: MyAppsCommon.jar MyAppsUtil.jar



This is because the application (EAR) class loader loads the MyAppsCommon.jar and MyAppsUtil.jar. The EJB class loader loads the MyAppsEJB.jar, which is the child class loader of the application class loader. The WAR class loader loads the MyAppsWeb.war.

Every J2EE application or EAR gets its own instance of the application class loader. This class loader is responsible for loading all the dependency jar files, which are shared by both WEB and EJB modules. For example third party libraries like log4j, utility classes, shared classes or common classes (Exception thrown by an EJB module should be caught by a WEB module) etc.

The key difference between the EJB and WAR class loader is that all the EJB jars in the application share the same EJB class loader whereas WAR files get their own class loader. This is because the EJBs have inherent relationship between one another (ie EJB-EJB communication between EJBs in different applications but hosted on the same JVM) but the Web modules do not. Every WAR file should be able to have its own WEB-INF/lib third party libraries and need to be able to load its own version of converted logon.jsp Servlet so each WEB module is isolated in its own class loader.

So if two different WEB modules want to use two different versions of the same EJB then we need to have two different ear files. As was discussed in the Q4 in Java section the class loaders use a delegation model where the child class loaders delegate the loading up the hierarchy to their parent before trying to load it itself only if the parent can�t load it. But with regards to WAR class loaders, some application servers provide a setting to turn this behaviour off (DelegationMode=false). This delegation mode is recommended in the Servlet 2.3 specification.

As a general rule classes should not be deployed higher in the hierarchy than they are supposed to exist. This is because if you move one class up the hierarchy then you will have to move other classes up the hierarchy as well. This is because classes loaded by the parent class loader can�t see the classes loaded by its child class loaders (uni-directional bottom-up visibility).

[ July 07, 2006: Message edited by: ak pillai ]
[ July 07, 2006: Message edited by: ak pillai ]

java j2ee job interview questions with answers | Learn the core concepts and the key areas
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Originally posted by Pradip Bhat:
Usually this is the order
1. Bootstrap classes like java.lang.String
2. Classes in java.ext.dirs
3. System classpath defined by CLASSPATH
4. AppServer libraries inside of some lib folder
5. For web app - lib folder first and then the classes folder.


Sorry, but most of that is incorrect. The WEB-INF/classes folder will always take precedence over jars in WEB-INF/lib, and the app-local classes take precendence over container-defined folders.

Again, refer to the Servlet Specification and your container documentation for precise details.
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

Thanks all !!!

Hi Bear ..can you reorder the list which is generally followed?

Regards
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Have you read the section of the Servlet Spec that I mentioned?
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Originally posted by Bear Bibeault:


Sorry, but most of that is incorrect. The WEB-INF/classes folder will always take precedence over jars in WEB-INF/lib, and the app-local classes take precendence over container-defined folders.

Again, refer to the Servlet Specification and your container documentation for precise details.


I agree that classes in WEB-INF/classes are loaded before WEB-INF/lib folder. For Webspshere the order as I mentioned holds good.
http://publib.boulder.ibm.com/infocenter/wasinfo/v5r0/topic/com.ibm.websphere.base.doc/info/aes/ae/crun_classload.html
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Order in which class is searched
 
Similar Threads
Connection Pooling Error
Search tool to search all class files
Question about ResourceBundle
Many Maven repositories in pom.xml or settings.xml mirrors
classpath and path