Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override some classes in a jar in WEB-INF/lib with another jar in JBoss EAP?

If the class to override is called com.example.FooServlet and this class is inside a jar WEB-INF/lib/foo.jar, how to override it with a class also called com.example.FooServlet in another jar, say bar.jar?

Or is there any way to make sure the one in bar.jar is loaded first?

Making bar.jar a module is no-go because the FooServlet imports tons of classes from many jars in WEB-INF/lib.


As I stated above, I tried to contain bar.jar in a module, but got class not found or no class def error (can't remember clearly) as FooServlet extends/implements some extra classes/interfaces which are in 3rd party jars in WEB-INF/lib.

I'm not allowed to touch foo.jar or any of the jars that are already existing in WEB-INF/lib.

like image 752
Dante WWWW Avatar asked Aug 18 '17 10:08

Dante WWWW


People also ask

How do you overwrite a class in a jar file?

JAR files are just ZIP files. Use a tool like WinZip to extract all the files from the JAR, replace the . class file with yours, recreate the ZIP file using e.g. WinZip, rename it to . jar, overwrite the original JAR file.

How can I see the class inside a jar file?

We can use this command with the t and f options to list the content of a JAR file: $ jar tf stripe-0.0. 1-SNAPSHOT. jar META-INF/ META-INF/MANIFEST.

Where is module XML in JBoss?

Installing a module on WildFly / JBoss EAP requires creating a path under the JBOSS_HOME/modules folder. Under this path, you will install the JAR libraries which are part of the module and a module. xml file which describes the module itself and dependencies with other module. Within the module.

What is the use of JBoss deployment structure XML?

JBoss Deployment Structure File xml is a JBoss specific deployment descriptor that can be used to control class loading in a fine grained manner. It should be placed in the top level deployment, in META-INF (or WEB-INF for web deployments). It can do the following: Prevent automatic dependencies from being added.


1 Answers

You said you cannot touch existing jars, and you seem to imply you can add a jar of yours to WEB-INF/lib.

According to this:

  • there is no specified order of precedence for jars under WEB-INF/lib/*.jar.
    So if you add bar.jar in there, you don't know if it will be loaded before or after foo.jar.
  • the servlet spec says classes under WEB-INF/classes must be loaded before anything under WEB-INF/lib/*.jar

Assuming you can add a jar under WEB-INF/lib, you should be able to add a class (or several) under WEB-INF/classes, without touching the ones in place.
So, if you want the classes from bar.jar to be loaded first, you can unzip the contents of that jar under WEB-INF/classes instead (or just the class you want to be loaded in priority -- for example WEB-INF/classes/com/example/FooServlet.class).

like image 184
Hugues M. Avatar answered Oct 20 '22 06:10

Hugues M.