WiredCoder
WiredCoder

Reputation: 926

Override Class loader "Parent First"

I have Java web application running on a web application server single node setup, in which I am using a liberary the I included in my Web-Inf and using in my code.

The issue is that I have another application that added its liberaries to the WebSphere parent lib folder, one of which are the same liberary I am using but with an older version, creating conflict and jamming my code.

The server class loader is configured Parent first unfourtunatly and I cannot change that fact. My question is, how can I make my app use my liberary, ignoring the one used by the class loader?

Upvotes: 2

Views: 3359

Answers (2)

Jarid
Jarid

Reputation: 2018

The solution is to move the conflicting package to a shared library, configure the library to use an isolated class loader, and associate that library with your application or module. The "isolated class loader" setting creates a separate parent-last class loader for the shared library, so you get that behavior targeted to only the artifacts that need it rather than having to apply it to the entire application or module.

https://www.ibm.com/support/knowledgecenter/en/SSAW57_8.5.5/com.ibm.websphere.nd.multiplatform.doc/ae/tcws_sharedlib.html

I'm specifically referencing the "Use an isolated class loader for this shared library" setting.

Upvotes: 10

Karol Dowbecki
Karol Dowbecki

Reputation: 44960

If you can't change your application server setup there are basically three things you can do:

  1. Downgrade your application dependency to lower version used by WebSphere server and keep it in sync. This is preferable as it's least hassle.

  2. Shade the dependency during build to your own package to prevent package clash. This can be done with Maven Shade Plugin, see Relocating Classes usage example.

  3. Write a new custom classloader to work around the problem.

I'd try them in 1 -> 2 -> 3 order. Option 3 is possible but is an error-prone nightmare. I'd rather deploy to another server than do it.

Upvotes: 2

Related Questions