0

I am deploying a JAR file to the Tomcat WEB-INF\lib folder and the classes or JavaBeans referenced in such JARs are being consumed properly by the other parts of Java code in the web application.

When I repeated the same steps on a different machine (UAT), with another similar Tomcat server instance (to test the application on UAT) I got an error indicating that the class is duplicated in two JAR files but I don't remember the exact error. I looked up the WEB-INF\lib folder and it took me some extensive effort to locate the culprit JAR file and remove it from the folder and everything worked fine.

The strange thing is that the same deployment is repeated on staging (to verify before production deployment) and I didn't get the error, and everything was fine, despite the fact that there was another JAR file with the same class and same package names.

Questions:

  • If there are multiple references to the same Java Class in more than one JAR files, how we can confirm which one is picked up by Tomcat server? I am able to verify by checking the log and searching for certain keywords I am sending to the log using debug statements. But, how to check that from Tomcat?

  • How I can search all the JARs in the lib folder for a possible duplicate reference of the Java class or package?
    Answer is provided here: https://stackoverflow.com/a/1343026

  • Why in one instance the duplicate class declaration error was thrown and in another instance, it was not thrown although it should have been thrown?

  • Is there a configuration to indicate if there are duplicate class declarations in multiple JARs to resolve the reference following certain criteria? Where is such config?

  • What are the recommended practices to deploy the JAR file with a new version of the class? Is it simple to copy/paste the new JAR and replace the existing file?

I appreciate your help. Tarek

tarekahf
  • 738
  • 1
  • 16
  • 42
  • Can you please share the exact error message from Tomcat? – D-FENS Dec 02 '21 at 06:22
  • I will try. I may have kept a snapshot of the error from Tomcat logs. I don't have it now. – tarekahf Dec 02 '21 at 06:23
  • In general having duplicate classes on the classpath should not necessarily lead to an error. Sometimes when two different versions of the same class have different method signatures or behavior it can cause runtime errors, should the wrong version be loaded first. I don't know if Tomcat treats the clashes any differently. – D-FENS Dec 02 '21 at 06:31
  • Deploy as a single war, not individual files. – Thorbjørn Ravn Andersen Dec 02 '21 at 11:13
  • As per the information available to me now, this option "Deploy as a single war" is not possible now. This is because I am dealing with a Workflow Platform based on Tomcat, and I am following the instructions to introduce customizations by developing JavaBeans and registering them by deploying JAR files. – tarekahf Dec 02 '21 at 15:38
  • @roccobaroccoSC I have added the most important question to this post. That is, how we can find out which JAR file was picked up by Tomcat? Just in case you have the answer. Please check the updated post. Thanks. – tarekahf Dec 03 '21 at 17:25
  • 1
    Then take a look at [this post](https://stackoverflow.com/questions/50611546/configure-tomcat-to-log-all-jar-files-and-or-classes-loaded-at-startup), it explains how to log the loading process: `I added -verbose:class to the JAVA_OPTS of the tomcat setenv.sh file` – D-FENS Dec 04 '21 at 09:09
  • Great thanks a lot. Now how come it worked in one instance and didn't in another? Is there circumstances where you can add two JAR files with the same class and it would work? – tarekahf Dec 05 '21 at 22:15

0 Answers0