1

A same class name with same package structure is residing in different jar files and both the classes are loadded by different class loader. if i want to import both the classes and use them in different scenario then how can I do this? Please let me know technique.

Eample :
ClassLoader :-TestClassLoaderFirst
package src.test.com;
class TestClass is present in A.jar 
TestClass{
   public void dispaly(){
      System.out.println("In A.jar ")
   }
}


and b.jar.
ClassLoader :-TestClassLoaderSecond
package src.test.com;
Class TestClass{
   public void present(){
      System.out.println("")
}
0

2 Answers 2

1

If you want to use those two classes in a strongly typed way from the same class then I believe you're out of luck.

If each class is only used from within one other class, separately, then you could compile each of those other classes separately, referencing the appropriate jar file, and then create a ClassLoader hierarchy so that at execution time each one ends up loading the right classes.

However, this is a complete pain. If at all possible, you should rename one of the classes to avoid the naming collision. I would be tempted to do that even if it meant rebuilding an open source project. (Change the name of the class from the project which is easiest to rebuild, to save time in the future.)

Sign up to request clarification or add additional context in comments.

2 Comments

If each class is only used from within one other class, separately, then you could compile each of those other classes separately, referencing the appropriate jar file, and then create a ClassLoader hierarchy so that at execution time each one ends up loading the right classes. As per the above statement What I understood that if i will use in two different classes then it can be possible. Could you please post any example for so that it will be more clear for me.
@Nishi: Not really, as it will depend on what sort of application you're writing, how the existing classloaders are operating etc. As I said, it would be far better to avoid the collision if you possibly can.
0

As Jon Skeet said, (and you really should listen a man with 327k!), don't do that. But if you must...
Assuming your classes look like

public class TestClass {
    public static void display() {
        System.out.println("In A.jar ");
    }
}

and get compiled into target/A.jar, and something like that happens to TestClass in B.jar, you can create a main class in your own package like this:

public class Main {

    public static void main(String[] args) throws MalformedURLException, IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException {
        URL urlA = new URL("file:target/A.jar");
        URL urlB = new URL("file:target/B.jar");
        URLClassLoader clA = new URLClassLoader(new URL[] { urlA });
        URLClassLoader clB = new URLClassLoader(new URL[] { urlB });

        clA.loadClass("TestClass").getMethod("display", (Class<?>[]) null).invoke(null, (Object[]) null);
        clB.loadClass("TestClass").getMethod("display", (Class<?>[]) null).invoke(null, (Object[]) null);
    }
}

and you will get

In A.jar
In B.jar

1 Comment

Pablo Grisafi@ If the JAR will be huge then it will give the performance impact. Could you please give any other alternative. If I used in different clesses then how can i use specific class in my class. Example: Class X will use the A.jar class and Class Y will use B.jar class. Please give me the example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.