Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect duplicate JARs in the classpath?

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?

like image 732
Aaron Digulla Avatar asked May 18 '09 14:05

Aaron Digulla


People also ask

How do I find the classpath of a jar file?

To check our CLASSPATH on Windows we can open a command prompt and type echo %CLASSPATH%. To check it on a Mac you need to open a terminal and type echo $CLASSPATH.

How can I tell what class a jar is loaded from?

The, the idea is to use find on the root of your classpath to locate all jars, then runs findclass.sh on all found jars to look for a match. It doesn't handle multi-directories, but if you carefully choose the root you can get it to work.

How do I find the classpath of a jar in Linux?

Another way of viewing the classpath is to run this Java code: String classpath = System. getProperty("java. class.


2 Answers

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
like image 175
Dave Webb Avatar answered Sep 26 '22 01:09

Dave Webb


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.

like image 28
Aaron Digulla Avatar answered Sep 23 '22 01:09

Aaron Digulla