Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Order of importing jars

Ranch Hand
Posts: 431
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IF we have a project using 2 external libraries. These 2 libraries will use the same jar file say abc.jar but with different versions 2.0 and 3.0. And, the main project also tries to import some class from these jar files which have different code base in the two versions may be some methods are updated or some logic is implemented differently. So, how the import and jar file version will be resolved in such cases? And, will it be possible to have both versions of jar files on the classpath?
Saloon Keeper
Posts: 12436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When loading a class, Java will use the class from the library it finds on the classpath first.
Posts: 25965
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would want to avoid that situation if you could. Perhaps by modifying the code which uses the old version, or something. But sure, it might happen that you can't deal with that problem because the code which uses the old version isn't under you control and doesn't have a path to upgrade it.

But no, you can't have both versions of abc.jar in the classpath.

Presumably the code which requires the old version can be segregated from the code which requires the new version -- if that isn't the case then it needs to be fixed. And you aren't going to have acquired that code from somebody else, because it won't run for them either, it's going to be your code.

So what's left is that you have some code, and some part of it requires the old version and some other part of it requires the new version.

The way to deal with that is to have one of the two parts use a separate classloader which loads classes from one of the two jars.

I'm not going to go into detail about how to do that because it may well be that you' re just asking a theoretical question and you don't actually have a request on your desk to make that project work. I will just say at this point that URLClassLoader is your friend.
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic