Arvind Katte
Arvind Katte

Reputation: 1003

What will happen if [WEB-INF/lib] and [tomcat/lib] contain similar JARs?

Today I was asked in an interview, what will happen at the time of deployment if the Tomcat's lib and your WEB-INF/lib of the war file contains same jar file?

I tested today in my machine, I didn't found any difference. Application is deploying successfully.

I have gone through with this similar article , For, more clarification if anyone is having idea about what will happen if the Tomcat's lib and WEB-INF/lib contains same jar what will happen ??

Upvotes: 1

Views: 908

Answers (2)

StackzOfZtuff
StackzOfZtuff

Reputation: 3106

Depends on delegate attribute

I haven't tried any of this. But it seems to hinge on value of the boolean delegate attribute.

And the way I read https://tomcat.apache.org/tomcat-10.0-doc/class-loader-howto.html#Class_Loader_Definitions is this:

For any webapp there are FOUR class loaders: bootstrap, system, common, webapp.

  • By default the boolean delegate attribute is not specified and will default to false
    • => order of class loaders: bootstrap, webapp, system, common
    • => Your webapp's JAR wins.
  • If, and only if, you explicitly specify <Loader delegate="true"/>
    • => order of class loaders: bootstrap, system, common, webapp
    • => common takes JARs from $CATALINA_BASE and $CATALINA_HOME
    • => Tomcat's JAR wins.

Further reading

Added info: This here is the documentation for the "delegate" attribute itself.

I find this slightly confusing, but it seems that Tomcat's default is to NOT follow Java's default. (Not sure wherever Java's default "delegation model" may have been defined.)

Quote from https://tomcat.apache.org/tomcat-10.0-doc/config/loader.html#Common_Attributes (Formatting changed for emphasis.)

  • Set to true if you want the class loader to follow the standard Java2 delegation model, and attempt to load classes from parent class loaders before looking inside the web application.

  • Set to false (the default) to have the class loader look inside the web application first, before asking parent class loaders to find requested classes or resources.

Upvotes: 1

Ramanlfc
Ramanlfc

Reputation: 8354

the jar contained in your WEB-INF/lib will be picked up by tomcat. The only caveat is core container jar's will be loaded in preference to jar's in WEB-INF/lib.

read : https://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html#Class_Loader_Definitions

Upvotes: 2

Related Questions