0

EDIT: I have a java class which calls two jars which has same class and method name. Can we implement like this ? how did the JVM know which right class to pick

import com.jar.Myclass; // should go to jar 1
import com.jar.Myclass; // should go to jar 2

public class Test {
public void getDetails(){
  if (true){
    Myclass.getDetails(); // should go and look in jar 1 
 }else {
    Myclass.getDetails(); // should go and look in jar 2 
  }
}

}

Any suggestion on this experts

Maana
  • 640
  • 3
  • 9
  • 22
  • Does this answer your question? [Change Name of Import in Java, or import two classes with the same name](https://stackoverflow.com/questions/2447880/change-name-of-import-in-java-or-import-two-classes-with-the-same-name) – Fildor May 06 '20 at 13:52
  • You need to use different packages for this. It is not working otherwise. You may get around this by using interfaces, reflection and resources but this is a bit of a complicated workaround. – dan1st May 06 '20 at 14:16
  • 1
    The best solution is to make the two classes have different fully-qualified names. Why do you have two classes with the same name anyway? – Slaw May 06 '20 at 15:01

1 Answers1

2

Edit: the question has since been edited to ask about two classes with identical FQCN. This answer does NOT apply to this new question.

If the simple name is the same but the package name is different then you should import one and fully quallify each reference to the other, or even fully quallify all references, for simplicities sake:

public class Test {
  public void getDetails(){
     if (true){
      com.jar1.MyClass.getDetails(); // should go and look in jar 1 
    }else {
      com.jar2.MyClass.getDetails(); // should go and look in jar 2 
    }
  }
}

Note that an import does nothing other than providing a simple short name (MyClass) to use instead of the fully-qualified (com.jar1.MyClass). To the runtime itself only the fully qualified class name (FQCN) exist. Imports are purely for the compiler.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • 1
    The question clearly specifies that they have the same fully qualified names. – David Conrad May 06 '20 at 14:12
  • @joachim Saucer- thanks for the response I have edited the question, in my scenario even the import name is same. – Maana May 06 '20 at 14:13
  • 2
    That's a very different question with a very different answer. Short answer: You'll have to do classloader shenanigans manually to do that. You won't just be able to reference both from within the same Java source file like this. – Joachim Sauer May 06 '20 at 14:18
  • 3
    @DavidConrad: that was a change that happened after I wrote the answer. – Joachim Sauer May 06 '20 at 14:20
  • @Joachim Saicer Is there a reference of class loader I can look into. – Maana May 06 '20 at 14:26
  • @Jay: [this question](https://stackoverflow.com/questions/11759414/java-how-to-load-different-versions-of-the-same-class) has a sketch for a similar situation, but it's not a full answer. I've not found an answer with a full description of how it works on a quick search. – Joachim Sauer May 06 '20 at 14:32