Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to load same class from different jars

Tags:

java

jar

I have a class Client.java in two different jars jar1 & jar2 Now at run time i want to decide which Client.class loaded like

if (country==india){
         // load Client class of jar1
) else{
        load client class from jar2 
}

can i do that...

like image 904
HK Agarwal Avatar asked May 18 '10 01:05

HK Agarwal


People also ask

Can I load a same class from two different JAR files in my one program?

You will need to create two additional class loaders, and put one of the jars in each. You will need to obtain a Class for the two classes, and you will need to use reflection to create the instance of the one you want..

Can we load the same class by two Classloader?

So when a class is loaded into JVM, you have an entry as (package, classname, classloader). Therefore the same class can be loaded twice by two different ClassLoader instances.

How do I import a class from a specific jar file?

One option is to call File. listFiles() on the File that denotes the folder, then iterate the resulting array. To traverse trees of nested folders, use recursion. Scanning the files of a JAR file can be done using the JarFile API ... and you don't need to recurse to traverse nested "folders".

Can a class have multiple versions of same method?

Yes, we can define multiple methods in a class with the same name but with different types of parameters. Which method is to get invoked will depend upon the parameters passed.


3 Answers

If the 2 classes have the same package name, i.e. com.mycompany.Client, then you end up in a situation where it is somewhat arbitrary which version of Client is loaded. It comes down to which is on the classpath first. This is a JAR hell situation http://en.wikipedia.org/wiki/Java_Classloader#JAR_hell.

This is a good situation to avoid but if you absolutely must have different versions of the same class, there are ways to do it. One way is to use a custom classloader and the classloader will know which version you need to do. This is not a trivial thing to do and can be difficult to manage. The OSGi framework is an alternative to help manage this (it uses custom classloaders under the hood), but I wouldn't use that if you just have one instance of a class as it is another framework that takes time and maintenance.

Bottom line: avoid the situation if you can and take the path of least resistance if you cannot.

If the classes do have different package names, @Casidiablo has provided a good answer.

like image 181
Jeff Storey Avatar answered Oct 08 '22 17:10

Jeff Storey


You will have to use the "complete" name of the path. For example:

if (country==india){
         name.first.package.Client client = new name.first.package.Client();
} else{
         name.second.package.Client client = new name.second.package.Client();
}

Anyway... I'd try to avoid doing things like that... that make your code difficult to read and mantain.

like image 41
Cristian Avatar answered Oct 08 '22 19:10

Cristian


If you really have two copies a class, with exactly the same fully-qualified name, in two jars, then ...

If you want to be safe, you should not put either of them in the classpath of your default class loader. You will need to create two additional class loaders, and put one of the jars in each. You will need to obtain a Class for the two classes, and you will need to use reflection to create the instance of the one you want..

like image 22
bmargulies Avatar answered Oct 08 '22 19:10

bmargulies