29

Does anyone have code to detect duplicate JARs in the classpath?

Background: When there are two versions of the same JAR in the classpath, really strange things can happen. This can even happen when using tools like Maven: Change a dependency and build the WAR without cleaning first. Since target/webapp/WEB-INF/lib wasn't cleaned, the dependency will be in there twice.

Is there a safety-net for this?

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • 1
    Verify if this can help you: http://stackoverflow.com/questions/17555928/remove-duplicate-jars-in-a-directroy/20611435#20611435 – Edgard Leal Dec 16 '13 at 13:16
  • 2
    Possible duplicate of [Find duplicated classes in classpath](https://stackoverflow.com/questions/12536482/find-duplicated-classes-in-classpath) – vorburger Oct 03 '18 at 23:40

5 Answers5

27

JBoss Tattletale might help you with this.

It's a free tool which scans the JAR files used by your project and gives you a report about them.

Amongst its feature are:

  • Spot if a class is located in multiple JAR files
  • Spot if the same JAR file is located in multiple locations
  • Find similar JAR files that have different version numbers
David Webb
  • 190,537
  • 57
  • 313
  • 299
6

There is a Maven plugin to do just that: maven-duplicate-finder-plugin

[EDIT] If you want to do it in unit tests yourself, use

getClass().getClassLoader().getResources( "com/pany/package/Java.class" )

If this returns more than one URL, you have duplicates on the classpath.

The drawback is that this only works for cases where you had conflicts in the past. On the positive side, it's just a few lines of code (or one line when you write a helper method) and it works on your build server.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
2

System.getProperty("java.class.path"), split it, sort it, look at it with the human eye :-).

It will not include the classpath derived from manifests inside other jars thou :-(.

Or use http://www.jboss.org/tattletale as one of the posters suggested.

Mihai Toader
  • 12,041
  • 1
  • 29
  • 33
  • This assumes that your dupes have the same name. Often the problem is duplicate classes in jars with different names. You'd need more than one pair of eyeballs there. – sal May 18 '09 at 16:21
  • Usually, the names will be different only in a version somewhere (usually a suffix). Programmatically you could detect and highlight common prefixes. – Lawrence Dol May 18 '09 at 17:59
  • You only need some sort of a warning sign in certain cases. What I proposed can be coded in 2 minutes and run as some sort of smoke test to catch "normal/easy" duplicates at the application startup. If you want extensive analysis use a tool like the tattletale proposed :-). – Mihai Toader May 18 '09 at 23:15
1

I think the simplest way is to simply trash the target directory first. Hopefully copying all the .jar files in isn't going to be time-consuming.

Otherwise you're going to have to somehow compare sizable files (whether directly, via computed checksum or similar). Which doesn't sound very nice at all.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
-2

You can write a simple script to compare the md5 sum of every jar file to every other jar file and deleting duplicates along the way.

neesh
  • 5,167
  • 6
  • 29
  • 32