CacheBuilder 中的 NoSuchMethodError (checkState)

NoSuchMethodError (checkState) in CacheBuilder

我创建了一个 Guava(一个包含有用内容的库,几乎可以在任何 Java 项目中使用,包括不可变集合库、函数式编程、I/O 等等)基于 CacheBuilder 的缓存

 LoadingCache<String, Byte[]> companyDevicesCache = 
             CacheBuilder.newBuilder()
                .maximumSize(1000)                     // maximum 100 records can be cached
                .expireAfterAccess(24, TimeUnit.HOURS) // cache will expire after 30 minutes of access
                .build(new CacheLoader<String, Byte[]>(){ // build the cacheloader

                   @Override
                   public Byte[] load(String companyId) throws Exception {
                      //make the expensive call
                      return getFromServletContext (companyId);
                   }


                });

我也试过了

CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES)
         .maximumSize(1000)
         .build(new CacheLoader<String, List<String>>() {
             @Override
             public List<String> load(String queryKey) throws Exception {
                 return null;
             }
         });

还有

CacheLoader loader = new CacheLoader()
            {
                public String load(String key) throws Exception
                {
                    return null;
                }

                @Override
                public Object load(Object arg0) throws Exception {
                    // TODO Auto-generated method stub
                    return null;
                }
            };

         LoadingCache<String, String> persons = CacheBuilder.newBuilder() 
                 .initialCapacity(30) 
                 .maximumSize(40) 
                 .recordStats() 
                 .build(loader);

final LoadingCache<String, Optional<Product>> cache =


               CacheBuilder.newBuilder()
                   .expireAfterWrite(1, TimeUnit.DAYS)
                   .refreshAfterWrite(1, TimeUnit.SECONDS)
                   .build( new CacheLoader<String, Optional<Product>>() {
                         @Override
                         public Optional<Product> load( String productId ) throws Exception {
                             return null;
                         }
                   }
             );

但是当我初始化 CacheBuilder 时出现了这个错误:

]] Root cause of ServletException.
java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;J)V
        at com.google.common.cache.CacheBuilder.maximumSize(CacheBuilder.java:409)

这是库的版本:guava-20.0-rc1.jar

这里是ear中包含的libs

我也尝试替换这个库 guava-19.0.jar 但后来我得到了

]] Root cause of ServletException.
java.lang.IncompatibleClassChangeError: Found interface com.google.common.base.Equivalence, but class was expected
        at com.google.common.cache.LocalCache$Strength.defaultEquivalence(LocalCache.java:393)
        at com.google.common.cache.CacheBuilder.getKeyEquivalence(CacheBuilder.java:297)
        at com.google.common.cache.LocalCache.<init>(LocalCache.java:246)
        at com.google.common.cache.LocalCache$LocalLoadingCache.<init>(LocalCache.java:4868)
        at com.google.common.cache.CacheBuilder.build(CacheBuilder.java:786)

看起来您在 class 路径上确实有多个版本的 com.google.common.base.Preconditions class(可能来自非常旧的 google-collections jar?),或损坏的罐子。

method overload which isn't found has been added in Guava 20.0:

public static void checkState(boolean b, @Nullable String errorMessageTemplate, long p1)

基于NoSuchMethodError中的签名:

checkState(ZLjava/lang/String;J)V
           ||                 | |
           ||                 | returns void
           ||                 |
           ||                 long
           |String
           |
           boolean

您可以通过在调用 CacheBuilder.maximumSize() 之前添加以下代码来找到 class 定义的来源:

System.out.println(com.google.common.base.Preconditions.class
        .getProtectionDomain()
        .getCodeSource()
        .getLocation()
        .toExternalForm());

你会得到罐子的URL。

我从 Maven Central 下载了 guava-20.0-rc1.jar,提取了它的内容并使用 javap 检查了 Preconditions.class 的内容,方法肯定在那里:

$ javap Preconditions.class
Compiled from "Preconditions.java"
public final class com.google.common.base.Preconditions {
  // ...
  public static void checkState(boolean, java.lang.String, long);
  // ...
}

在你的ant代码段中,有多个通配符包含,例如:

<include name="**/spring/*.jar"/>

那里可能有额外版本的 Guava,尤其是考虑到切换到 guava-19.0 时出现的其他错误:Equivalence 是 10.0 的 interface until 9.0, and was changed in 2011 to a class