2

I'm coding in my current package, and take two packages/project as the dependencies which are named as package-A and package-B.

In package-A, there is an java class: "com.xx.cc.java" and in package-B, there is an same-name java class: "com.xx.cc.java" (fields and function in these two java class are different)

I want to import the "com.xx.cc.java" in package-A, however, eclipse would automatically import the "com.xx.cc.java" in package-B, I'm really boring about it and have no idea.

Is there anyway to specify the package in which I want to import the java class ?

Thanks a lot in advance.

Junjie
  • 1,145
  • 3
  • 21
  • 37
  • You mean that you import these packages from 2 different jars, right? – Luiggi Mendoza Jun 10 '14 at 14:50
  • Yeah, from 2 different jars, and I have no permission to change any of them. – Junjie Jun 10 '14 at 14:53
  • only thing what i can think off is to change order of your libraries in project properties, but i'm not sure will this work, to be honest, im not sure is using two libraries which implements same class in same pachage with different functionality is good idea in first place – user902383 Jun 10 '14 at 14:58
  • @user902383 the order of jars won't matter, the problem will be there. But as you said in the end, having this mess is not a good idea. – Luiggi Mendoza Jun 10 '14 at 14:59
  • you can actually try load your class on runtime from specified jar file/project, that might work – user902383 Jun 10 '14 at 15:05
  • I'm really suffering with the mess design, however, I have no permission to change any of them. – Junjie Jun 10 '14 at 15:16
  • @user902383 Both of these two packages are required to me, does it matter the usage of them from your idea ? – Junjie Jun 10 '14 at 15:18
  • OT: The architects or designers of this application should come here and have a talk with us :). Basically, there's no way to solve it unless you load/unload classes at runtime, but this is a really bad design and should be avoided. – Luiggi Mendoza Jun 10 '14 at 15:35
  • @LuiggiMendoza Bad news, but, thank you. – Junjie Jun 10 '14 at 15:40

3 Answers3

2

Theoretically you can solve this problem using different class loaders for each one of the libraries. But you cannot put both into application class path. At least one of them should be beyond the regular application classpath and you should implement access to it using your custom class loader.

How to solve this in eclipse? You can create 2 projects. One of them depends on library A, other on library B. The "other" project will implement the custom class loader that I have already mentioned.

BUT: do you really need this? What are these strange libraries that put different classes to the same package? This situation sounds bad from the beginning.

AlexR
  • 114,158
  • 16
  • 130
  • 208
0

You are simply going to have difficult to determine behavior at runtime when there are multiple classes with the same fully qualified name. You can attempt to control the order of jars/resources on the classpath. However, it sounds like perhaps you are running in an osgi environment, where you have very little control over order of items on class path.

Brett Okken
  • 6,210
  • 1
  • 19
  • 25
-2

Yes, there is a way to access the classes with same name lying in different packages.

Let assume class A in package1 and package2. Then to access the two classes A in another class, do as follows:

  • Import the two packages: package1 & package2

  • Use the classes as package1.A & package2.A

Hope it helps you.

michaelbn
  • 7,393
  • 3
  • 33
  • 46
  • Thanks Abhishek, however, the package path of these two class are the same, so it doesn't work. – Junjie Jun 10 '14 at 19:00
  • Read the question + comments. The classes have the *exact same* fully-qualified name. – awksp Jun 10 '14 at 19:25
  • jjs I still can't get your question exactly. Can you please clarify it. – Abhishek Srivastava Jun 11 '14 at 17:54
  • Hello jjs, you want to access the class with a certain name in one dependency package without using or importing the class with same qualified name present in other dependency package, right? – Abhishek Srivastava Jun 11 '14 at 18:28
  • Sorry for late response. I have two dependency package(jars) A and B, Both of package A and package B have java class named as com.example.a.java, that means, if I want to import this class from package A, I need to code "import com.example.a;", it is absolutely same with the importing from package B. It's not stable. That's why there is this question. – Junjie Jun 12 '14 at 17:41