Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding a class file from a library JAR in a Java web app

In my Java web app, I have these files:

/WEB-INF/classes/com/example/Foo.class
/WEB-INF/lib/example.jar <-- contains the class "com.example.Foo"

Will the class defined in the classes directory be used instead of the class defined in example.jar?

Thanks.

like image 725
Michael Avatar asked Dec 02 '11 20:12

Michael


1 Answers

The answer depends upon your container, it is container dependent. In general, /WEB-INF/classes is preferred to classes in a jar file in WEB-INF/lib.

For Tomcat, the order is the following:

Therefore, from the perspective of a web application, class or resource loading looks in the following repositories, in this order:

  1. Bootstrap classes of your JVM
  2. System class loader classes
  3. /WEB-INF/classes of your web application
  4. /WEB-INF/lib/*.jar of your web application
  5. Common class loader classes

But if you're using a different container, you need to consult the documentation for that server.

like image 82
Matthew Farwell Avatar answered Oct 26 '22 19:10

Matthew Farwell