aspose file tools*
The moose likes Java in General and the fly likes conflict due class is having same name as well as same package structure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "conflict due class is having same name as well as same package structure" Watch "conflict due class is having same name as well as same package structure" New topic
Author

conflict due class is having same name as well as same package structure

Mahesh Bamane
Ranch Hand

Joined: Mar 12, 2008
Posts: 68
Well, I've been dealing with a big problem. I have two classes which has same class name and package structure they are present in two different jars for sure.
When I call a method of one class say for example setName.
I get an exception saying noSuchMethod; what can be the possible soluntion in this regard.
As classes are in jars I cant make any changes to them.


There is nothing like trying, either you do it or you don't.
SCJP 1.5
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Do these classes do the same thing? If so, remove one of the classes from its jar.
If they do different things, you will have to load both classes in a different class loader.
Mahesh Bamane
Ranch Hand

Joined: Mar 12, 2008
Posts: 68
Thanks Stephan, I didn't get loading two classed using different class loader. Any information on this will be helpful.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

It's going to be tricky, and dirty, and it will make your code very confusing. Only use it when you really don't have any other option. Can you first explain why the two jars contain classes with the same name, and whether the classes are actually the same, or if they perform different functions? A bit more detail about your project would be helpful.
Mahesh Bamane
Ranch Hand

Joined: Mar 12, 2008
Posts: 68
well, to reply your question those classes are nt identical and having the same class name and package structure is out of my control.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

In this case you might want to build a wrapper class for the class you want to use. The idea is that you first create a custom class loader that finds the specific class you want. You can then create instances of this class and call methods on it reflectively through the Class object. Create a wrapper class that delegates to these reflective method calls, so it will make the rest of your code easier to use.

I will try to flesh out an example wrapper for you, so you can see what I mean.
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

Rob Spoor wrote:You don't even need to rename them; JAR files can be opened with all ZIP applications just fine.

That is so true except some of archiving utilities does not allow to make changes in archive if it's not renamed to zip (for example, WinRar).
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

[edit]

The Wrapper class below is *broken*. Do not use it. The class loader it uses will not be able to locate classes referenced by the wrapper class.
Instead, it's better to create a custom class loader at start up, that takes the responsibility of finding all the classes the program references, and keeping the two conflicting classes separateed.

[/edit]

Let's say we have two different classes with the same name, in two different Jars:
The first one is located in Lib1.jar, the second one is located in Lib2.jar. Both are in the classpath.

In my case, I can't use the second class, because the first one is being loaded by the class loader:Invoking this program will give me a NoSuchMethodException, if I get it to compile in the first place. So if I want to use the second class, I have to write a wrapper class for it:
This wrapper provides exactly the same methods as the class I want to use, and it provides a factory class that can provide instances of the wrapper class. Using this class works as follows:
 
Consider Paul's rocket mass heater.
 
subject: conflict due class is having same name as well as same package structure