How to identify duplicate classes in a java application
search cancel

How to identify duplicate classes in a java application

book

Article ID: 293326

calendar_today

Updated On:

Products

VMware Tanzu tc Server

Issue/Introduction

A java application fails to start with classloading error. Tomcat 9 load classes is random and the error occurs when the wrong class/jar gets loaded first.

This KB will provide steps to identify the duplicate classes/jars.

Cause

This is usually due to having same class in multiple jar. 

Resolution

To find jar having duplicate classes, you can do the following steps:

  1. Change directory to the application LIB folder

    $ cd <PATH-webapp>/<APP-NAME>/WEB-INF/lib


  2. Extract the duplicate classes and redirect to a file (example: CLASSES.TXT)

    $ ls *.jar | xargs -n1 -IFILE unzip -l FILE | grep class | sed "s,.* ,," | tr "/" "." | grep -v module-info.class | sort | uniq -d > CLASSES.TXT


  3. Find the jars

    $ for line in $(cat CLASSES.TXT); do echo -----$line; grep -iRH $line *.jar ; done


  4. Sample output:

    $ for line in $(cat CLASSES.TXT); do echo -----$line; grep -iRH $line *.jar ; done
    -----org.springframework.boot.actuate.amqp.RabbitHealthIndicator.class
    Binary file dup-spring-boot-actuator-2.2.2.RELEASE.jar matches
    Binary file spring-boot-actuator-2.2.2.RELEASE.jar matches
    -----org.springframework.boot.actuate.audit.AuditEvent.class
    Binary file dup-spring-boot-actuator-2.2.2.RELEASE.jar matches
    Binary file spring-boot-actuator-2.2.2.RELEASE.jar matches
    --

 

Once you have the duplicate jars you can decide which one to keep/delete. Our recommendation is to delete/remove the unused (or old) jars from the app.