Is there any way to solve this problem?
In principle, no.
As for JLS, types are different types, and the JVM doesn't let you pretend otherwise. For example, classes can have different code and different layouts of objects. If you could trick the JVM into treating the types as the same, you could deflate the security of the JVM environment. This is insanity.
The solution is to make sure that you do not have two different class loaders loading the same class. In the context of Tomcat, this means that if two or more web folders need to share instances of a class, then this class must be defined in the class loader, which is common to both; for example, put the JAR file in the $CATALINA_HOME/lib
or $CATALINA_HOME/common
.
If there is a good reason why classes should be loaded by different class loaders (perhaps because the classes are really different), then you can solve this problem by specifying the interface that both versions implement the class, and then programming to the interface, not to the class implementation. Of course, there can only be one version of the loaded interface ... or again you will encounter the same problem again.
source share