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?
It is simple to import any library that is referenced in the Project properties. Add library in the project using properties then import that. But net it built-in library provided in JDK and JRE. You can use import java.net.
In every java class, java. lang is imported by default.
To import java package into a class, we need to use java import keyword which is used to access package and its classes into the java program. Use import to access built-in and user-defined packages into your java source file so that your class can refer to a class that is in another package by directly using its name.
For convenience, the Java compiler automatically imports two entire packages for each source file: (1) the java. lang package and (2) the current package (the package for the current file).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With