简体   繁体   English

Guava java EventBus实例化错误

[英]Guava java EventBus instantiation error

I'm trying to use EventBus of Google's Guava libraries. 我正在尝试使用Google的Guava库的EventBus。

From Guava's documentation it should be easy to instantiate an EventBus object. 从Guava的文档中可以很容易地实例化EventBus对象。

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. 我尝试过使用Guava 10.0,11.0和12.0并且总是出现同样的错误。 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. 我在OSX Lion上,我正在使用Netbeans 7.1:我已经尝试过Java 6(32和64位)和Java 7:没有任何改进。 On google i can't find anything. 在谷歌我找不到任何东西。 Is it a problem with Guava? 这是番石榴的问题吗? Or, as usually, am I missing something? 或者,通常,我错过了什么?

Best regards, 最好的祝福,

Alessandro 亚历山德罗

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. 为了扩展@biziclop所说的内容,你最有可能同时拥有最新版本的Guava以及google-collect或类似路径上的3.0之前的Guava版本。 Objects.firstNonNull was added in 3.0, suggesting that an old version of that class is being loaded. Objects.firstNonNull在3.0中添加,表明正在加载该类的旧版本。

I had the same problem. 我有同样的问题。 I was using google-collections 1.0 where guava is v11. 我正在使用google-collections 1.0,其中guava是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 <!-- https://mvnrepository.com/artifact/com.google.guava/guava --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency>来自

<dependency>
  <groupId>com.google.collections</groupId>
  <artifactId>google-collections</artifactId>
  <version>1.0</version>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM