Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When importing classes from two conflicting jars, which one is used?

Tags:

java

import

jar

In Java, I recently faced a case where I was getting two different jars that each defined a class. The problem was that one of these jars was out of date and the class in question was missing a method that existed in one jar and not the other.

So, I was getting an error that the method being used in the code couldn't be found. I was eventually able to resolve this by removing the old jar, so that it imported the correct one.

Many people used this same code (with the same two, conflicting, imported jars) and did not have this problem. So, they must have been importing the up-to-date jar.

My question is this: What caused me to import one jar over another? What logic determines which is "used"?

Thanks!

like image 266
Jeremy Avatar asked Jan 16 '23 20:01

Jeremy


1 Answers

Based on the order. The first one will be used and the second one will start causing issues.

Make sure you don't include different versions of the same class. You may encounter weird bugs because of that.

like image 136
Sully Avatar answered Jan 19 '23 11:01

Sully