9

I have two versions of a Java library model.jar, each with the same set of classes (but different implementations). I want to write a Java class that imports some classes from one version, and imports some from the other version.

I know I can include both in compilation by giving them different names:

javac -cp model.jar;model2.jar MyClass.java

But any import statement will import the corresponding class from the first .jar file I specify in the classpath.

Can I specify in my import statement which library to import from, given that the library structure will be the same for both files?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
notmichael
  • 91
  • 5
  • That's a good question. On the other hand: the path to a file complete with the classname should make clear what it is meant for and what it is to be used for. Why would you have two classes with an identical path/classname? – Stultuske Apr 05 '16 at 08:15
  • Not an answer but this looks like a kind of bad practice. (@Stultuske OP has two differents versions of the same library) – Apolo Apr 05 '16 at 08:17
  • AFAIK, this is simply not possible. – Ji aSH Apr 05 '16 at 08:18
  • I'm using an older version of the same library, thus with the same paths/classnames. – notmichael Apr 05 '16 at 08:18
  • I am not sure but there might be RuntimeExceptions if we have two classes with same path and classname but in different jars. – Praveen Kumar Apr 05 '16 at 08:18
  • 2
    You can try different classloaders for each module and use those classloaders to access the correct versions but it still is kind of bad design and receipe for disaster. Try common interfaces and implementations in different packages instead. – Thomas Apr 05 '16 at 08:18
  • Why aren't you just using the newer version of that library? – Thomas Apr 05 '16 at 08:19
  • @Thomas The newer version has removed some methods that I need for certain classes. – notmichael Apr 05 '16 at 08:22
  • @Apolo I understand the 'what', it's the 'why' that I don't get. – Stultuske Apr 05 '16 at 08:23
  • @notmichael so don't use the newer version. or: check why they were removed. most likely for a good reason. – Stultuske Apr 05 '16 at 08:24
  • @Stultuske The newer version has also added some methods I need, hence the need to take from both. – notmichael Apr 05 '16 at 08:25
  • @notmichael: my advice: check the newer version and see with what the removed methods have been replaced and use those. writing to older versions (not to mention two versions mixed) will eventually lead to problems – Stultuske Apr 05 '16 at 08:29

3 Answers3

3

It is not possible (well actually, at least not that simply).

Depending on what you are trying to achieve, if you really have to use two versions of a library, you can try a module system like OSGi, Jboss-Modules, or something like that.

Community
  • 1
  • 1
user140547
  • 7,750
  • 3
  • 28
  • 80
2

If the classes have the same name (including the package) there is no chance unless you work with different class loaders. Java will always use the first matching class it finds on the class path.

Moreover, nasty problems may arise if you use the same library in different versions. You should really try to resolve this in another way.

Henry
  • 42,982
  • 7
  • 68
  • 84
1

You can't like this.

The only thing you can do is to use a script to generate one jar from the two with the classes that you want, and put that jar in the classpath

Walfrat
  • 5,363
  • 1
  • 16
  • 35