0

I have two libraries that both follow the same names for importing, but one has more functionality than the other. How do I make scala choose the right library to use when importing? Currently, it’s choosing the one that I don’t want which has less functionality.

I have similar code in java and it’s able to grab the correct library when I import it there, just seem to have an issue with scala.

Example:

Library 1 import (Less functionality): (package).Mail
Library 2 import (More functionality): (same package name).Mail

Note: I am using maven as my dependency manager and I can’t get rid of the library I don’t want as a lot of the code currently depends on it. I also can’t change it in any way as the code is not managed by our team.

With the autocomplete feature in IntelliJ I see 2 options with the same name when filling in the import. I’ve tried clicking on both options, but they both link back to the one I don’t want.

11
  • 1
    Each lib has the same group ID and artifact ID? Maybe different version? Is it an explicit import? Transient? Could you show your pom.xml? Commented Jul 3 at 19:15
  • 2
    There's something missing here, as JVM's ClassLoader would not allow 2 classes of the same name in the same package. No matter if it's Java, Scala, Groovy, Clojure etc. Commented Jul 3 at 19:41
  • 1
    It can be hard to predict the behavior when you have the same class name in your classpath multiple times. The one that will be loaded depends on the class loader implementation that is used to look the class up, and that will generally depend on the order of classpath entries it receives, which is not always easy to control from application code. It’s best to avoid this by excluding the undesired version from the dependencies.
    – Tim Moore
    Commented Jul 4 at 1:27
  • 3
    In other words, in the Java application that appears to work correctly, it is probably only working by sheer coincidence. Any kind of incidental change that results in Maven resolving dependencies in a different order could break it.
    – Tim Moore
    Commented Jul 4 at 1:29
  • 2
    Unless you do some very high level classloader magic you are basically in a very bad place. The proper solution is to either get rid of one of them or put them in different packages so they are unique again. Tools can help with this. How much ownership do you have of these libraries. Commented Jul 4 at 8:12

0

Browse other questions tagged or ask your own question.