0

I've used a 17 version of Guava for my library project and extracted it into the jar. Other project uses my library but it also has a much older version of Guava. When I try to run application, it ignores new Guava and causes errors like

java.lang.NoSuchMethodError: com.google.common.cache.CacheBuilder.maximumSize(J)Lcom/google/common/cache/CacheBuilder;

How to force my project to use a new Guava and application to use an old?

TeroBlaZe
  • 75
  • 2
  • 10

1 Answers1

1

I am assuming that your project and applications are distinct entities.Also, by Project I assume that you are referring to an Eclipse/IntelliJ/Netbeans project

You would need to set the correct classpaths for the Project and your application separately.

If you are using the IDE to debug your project, your project would need to include the new JAR that you have downloaded. Each IDE has it's own mechanism for adding dependencies to the Project's classpath and hence you would want to refer to the documentation related to that IDE.

For your application you can launch it as java MyApp -cp "path to the old JAR". If you are packaging your application as a JAR, make sure you are packaging the older version of Guava.

Hope this helps.

Edit based on the discussion with the owner of the question

The query here is similar to this SO query. The right way to hence resolve this issue would be either install a Custom ClassLoader or use OSGi

Community
  • 1
  • 1
Prahalad Deshpande
  • 4,709
  • 1
  • 20
  • 22
  • Not quite. The Main application contains old guava and it supports plugins, I've made a plugin for it using my library that also contains guava but newer version. – TeroBlaZe Jun 01 '14 at 15:32
  • @TeroBlaZe Then did you try Natan Cox' suggestion of putting Guava 17 before the older version in your classpath? – Prahalad Deshpande Jun 01 '14 at 16:30
  • No, because I haven't using a classpath for running an application. The compiled "Main application" has all dependencies extracted inside, and when I connect my library with same but newer dependency in the plugin, running application ignores local guava and uses that the application has. For example: `Application using: [Guava old, GSON, etc..] <=> Plugin using: [ MyLib using: [Guava new] ]` – TeroBlaZe Jun 01 '14 at 16:50
  • 1
    @TeroBlaZe It means that you are using two versions of the same library within the project. Check if the solution mentioned here helps you - http://stackoverflow.com/questions/1553567/java-classloader-how-to-reference-different-versions-of-a-jar. I will then update my answer – Prahalad Deshpande Jun 01 '14 at 16:59
  • Gosh... I think it's not worth it. I looking for more simple and effective way, maybe fork guava to another package? xD – TeroBlaZe Jun 01 '14 at 17:32
  • 2
    @TeroBlaZe As per the query here : http://stackoverflow.com/questions/7240529/java-use-two-version-of-the-same-lib-in-one-webapp, you have entered into a "JAR Hell". One of the possible approaches suggested is to use a tool like jarjar : https://code.google.com/p/jarjar/ that would rename the packages – Prahalad Deshpande Jun 01 '14 at 17:39