Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I choose which library a class will be imported from in Java?

Tags:

java

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?

like image 897
notmichael Avatar asked Apr 05 '16 08:04

notmichael


People also ask

How are libraries imported in java?

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.

Which library of classes is automatically brought into every java file?

In every java class, java. lang is imported by default.

How can class be imported from a package to a program?

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.

Which package is imported by default in Java class?

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).


1 Answers

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.

like image 68
user140547 Avatar answered Oct 05 '22 08:10

user140547