Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guava java EventBus instantiation error

I'm trying to use EventBus of Google's Guava libraries.

From Guava's documentation it should be easy to instantiate an EventBus object.

My code:

package test;

import com.google.common.eventbus.EventBus;

public class Test {
    public static void main(String[] args) {
        EventBus bus = new EventBus("Sample");
    }
}

I'm getting this error:

Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.base.Objects.firstNonNull(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
at com.google.common.cache.CacheBuilder.getKeyStrength(CacheBuilder.java:355)
at com.google.common.cache.CustomConcurrentHashMap.<init>(CustomConcurrentHashMap.java:206)
at com.google.common.cache.ComputingCache.<init>(ComputingCache.java:39)
at com.google.common.cache.CacheBuilder.build(CacheBuilder.java:569)
at com.google.common.eventbus.EventBus.<init>(EventBus.java:156)
at test.Test.main(Test.java:7)
Java Result: 1

I've tried with Guava 10.0, 11.0 and 12.0 and always the same error. I'm on OSX Lion and I'm using Netbeans 7.1: I've tried both Java 6 (32 and 64bit) and Java 7: no improvements. On google i can't find anything. Is it a problem with Guava? Or, as usually, am I missing something?

Best regards,

Alessandro

like image 854
Alejander Avatar asked Jul 09 '12 16:07

Alejander


2 Answers

To expand on what @biziclop said, you most likely have both a recent version of Guava and either google-collect or a version of Guava prior to 3.0 on your classpath. Objects.firstNonNull was added in 3.0, suggesting that an old version of that class is being loaded.

like image 123
ColinD Avatar answered Nov 14 '22 13:11

ColinD


I had the same problem. I was using google-collections 1.0 where guava is v11. This problem went away after I upgraded to

<!-- https://mvnrepository.com/artifact/com.google.guava/guava --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency> from

<dependency>
  <groupId>com.google.collections</groupId>
  <artifactId>google-collections</artifactId>
  <version>1.0</version>
</dependency>
like image 3
jprism Avatar answered Nov 14 '22 13:11

jprism