• Post Reply Bookmark Topic Watch Topic
  • New Topic

Two Ja file having the same class file but different implementation.How to use both jar file?  RSS feed

 
Bhavik Goswami
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two jar contains same class(HttpTransport.class) under the same package name but having different implementation, How to solve the problem?

I am using two jar files : 1) google-api-client-1.4.1-beta.jar 2) gdata-2.2.1-alpha-client-http.jar


Both the jar files contains HttpTransport.class but having the different implementaion. But the package name for both are the same i.e. com.google.api.client.http.HttpTransport.
But It creates the problem to use the class of the second jar file. I Need both the jar file so i can not remove any jar file from my project. Can anyone guide me how to solve this problem without delete any jar file.

Regards,
Bhavik
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no simple solution for this problem.

Java's mechanism for finding classes is very simple. Everything that is in the classpath (directories and JAR files) is put into a list, and when Java looks for a class, it just takes the first one that it can find in the list. There is nothing to handle the problem of having multiple classes that have the same name and that are in the same package.

There might be a way to do this with a custom ClassLoader, but it will not be easy. Another solution is to use a module system such as OSGi, which allows you to have different parts of your application as OSGi bundles; each bundle can have its own dependencies on different versions of the same library.

To fix this problem, I would first investigate if it is absolutely necessary to have google-api-client-1.4.1-beta.jar. Can't the rest of the application use the HttpTransport class that is in gdata-2.2.1-alpha-client-http.jar? (Or the other way around?)

Is this a project for a real business system? I wouldn't use beta- and alpha-versions of libraries for real production code. Aren't there other versions of those libraries available that are matched, so that you don't get this problem?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!