How to load the same class from different cans

I have a Client.java class in two different banks jar1 and jar2. Now, at runtime, I want to decide which Client.class is loaded as

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

Can i do this ...

+5
source share
4 answers

If 2 classes have the same package name, that is com.mycompany.Client, then you are in a situation where it is somewhat arbitrary, which version of the client is loading. This boils down to what primarily refers to the class. This is a hellish JAR situation http://en.wikipedia.org/wiki/Java_Classloader#JAR_hell .

, , , . - , , . , . OSGi ( ), , , , .

: , , , .

, @Casidiablo .

+7

"" . :

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

... ... .

+4

, , ...

, . . , , , .

+3

, 2 :

1) , : IndiaClient Country2Client;

    interface Client {...}
    class IndiaClient implements Client {...}
    class Country2Client implements Client {...}

    Client client;
    if (country==india){
        client = new IndiaClient();
    ) else{
        client = new Country2Client();
    }

2) jar, - ClassLoaders :

    interface IClient {...}
    class Client implements IClient {...} // in jar1
    class Client implements IClient {...} // in jar2

    Class<IClient> clientClass;
    if (country==india){
        clientClass = classLoader1.loadClass ("package.Client");
    ) else {
        clientClass = classLoader2.loadClass ("package.Client");
    }
    IClient client = clientClass.newInstance ();

  • How to get the Loaders class, you can refer to the JDK docs.
0
source

All Articles