java NoSuchMethodError: com.google.common.base.Stopwatch.createStarted()Lcom/google/common/base/Stopwatch

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28855559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:17:47  来源:igfitidea点击:

NoSuchMethodError: com.google.common.base.Stopwatch.createStarted()Lcom/google/common/base/Stopwatch

javaguavarestletapispark

提问by quarks

My app is throwing NoSuchMethodError: com.google.common.base.Stopwatch.createStarted()Lcom/google/common/base/Stopwatcherror. Not sure why, because 16.0.1 do contain that class, I've checked. From what I have researched, it looks like this is a bug?

我的应用程序抛出NoSuchMethodError: com.google.common.base.Stopwatch.createStarted()Lcom/google/common/base/Stopwatch错误。不知道为什么,因为 16.0.1 确实包含那个类,我已经检查过了。根据我的研究,这看起来像是一个错误?

I also have this code for refernence, though I think this is not the issue:

我也有这个代码供参考,虽然我认为这不是问题:

    FirewallRule rule = new PeriodicFirewallCounterRule(60, TimeUnit.SECONDS, new IpAddressCountingPolicy());
    ((PeriodicFirewallCounterRule)rule).addHandler(new RateLimitationHandler(new UniqueLimitPolicy(10)));
    FirewallFilter firewallFiler = new FirewallFilter(getContext(), list(rule));
    firewallFiler.setNext(ma);

My app is using Restlet APISpark:

我的应用程序正在使用 Restlet APISpark:

  <dependency>
      <groupId>org.restlet.gae</groupId>
      <artifactId>org.restlet.ext.apispark</artifactId>
      <version>${version.restlet}</version>
  </dependency>

When running and accessing the REST api of the app, it throws this error:

在运行和访问应用程序的 REST api 时,它会抛出此错误:

[INFO] Caused by: java.lang.NoSuchMethodError: com.google.common.base.Stopwatch.createStarted()Lcom/google/common/base/Stopwatch;
[INFO]  at org.restlet.ext.apispark.internal.firewall.rule.counter.PeriodicCounter.<init>(PeriodicCounter.java:65)
[INFO]  at org.restlet.ext.apispark.internal.firewall.rule.PeriodicFirewallCounterRule.load(PeriodicFirewallCounterRule.java:86)
[INFO]  at org.restlet.ext.apispark.internal.firewall.rule.PeriodicFirewallCounterRule.load(PeriodicFirewallCounterRule.java:84)
[INFO]  at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3599)
[INFO]  at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2379)
[INFO]  at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2342)
[INFO]  at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2257)
[INFO]  ... 74 more

采纳答案by Thierry Templier

When using the extension org.restlet.ext.apispark, the guava dependency retrieved has the version 16.0.1.

使用扩展程序时org.restlet.ext.apispark,检索到的番石榴依赖项具有版本16.0.1.

Downloading: http://maven.restlet.com/com/google/guava/guava/16.0.1/guava-16.0.1.jar
Downloading: http://repo.maven.apache.org/maven2/com/google/guava/guava/16.0.1/guava-16.0.1.jar
Downloaded: http://repo.maven.apache.org/maven2/com/google/guava/guava/16.0.1/guava-16.0.1.jar (2176 KB at 711.7 KB/sec)

It comes within an application created from scratch with the following maven configuration:

它来自一个从头开始创建的应用程序,具有以下 Maven 配置:

<project (...)>
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.restlet</groupId>
    <artifactId>restlet-apispark-firewall</artifactId>
    <name>${project.artifactId}</name>
    <packaging>jar</packaging>
    <version>1.0.0-snapshot</version>

    <properties>
        <java-version>1.7</java-version>
        <restlet-version>2.3.1</restlet-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.restlet.jse</groupId>
            <artifactId>org.restlet</artifactId>
            <version>${restlet-version}</version>
        </dependency>

        <dependency>
            <groupId>org.restlet.jse</groupId>
            <artifactId>org.restlet.ext.apispark</artifactId>
            <version>${restlet-version}</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>maven-restlet</id>
            <name>Public online Restlet repository</name>
            <url>http://maven.restlet.com</url>
        </repository>
    </repositories>
</project>

I integrated your code and it works fine on my side. No exception is thrown...

我集成了你的代码,它在我这边工作正常。没有异常抛出...

I think that an older version of Guava comes from another dependency. If you use Maven, you should identify where this old guava version comes from and perhaps add an exclude within the corresponding dependency. I hope that it will fix your problem...

我认为旧版本的 Guava 来自另一个依赖项。如果您使用 Maven,您应该确定这个旧番石榴版本的来源,并可能在相应的依赖项中添加一个排除项。我希望它能解决你的问题......

Hope that helps you, Thierry

希望能帮到你,蒂埃里

回答by quarks

This is the solution that fixed the error:

这是修复错误的解决方案:

First exclude old Guavadependency then:

首先排除旧的Guava依赖然后:

  <dependency>
      <groupId>org.restlet.gae</groupId>
      <artifactId>org.restlet.ext.apispark</artifactId>
      <version>${version.restlet}</version>
      <exclusions>
          <exclusion>
              <groupId>com.fasterxml.Hymanson.core</groupId>
              <artifactId>Hymanson-databind</artifactId>
          </exclusion>
      </exclusions>
  </dependency>
  <dependency>
      <groupId>com.fasterxml.Hymanson.core</groupId>
      <artifactId>Hymanson-databind</artifactId>
      <version>2.5.1</version>
  </dependency>

回答by Umberto Raimondi

The class Stopwatch loaded by that ClassLoader does not contain that method, not sure if caused by multiple incompatible jars as Jens says or simply because 16.0.1 does not really have that method. A simple check will be to parse the class with javap or a decompiler:

该 ClassLoader 加载的类 Stopwatch 不包含该方法,不确定是由 Jens 所说的多个不兼容的 jars 引起的,还是仅仅因为 16.0.1 没有真正具有该方法。一个简单的检查是使用 javap 或反编译器解析类:

javap -p Stopwatch.class

And then check if that method is listed.

然后检查是否列出了该方法。

Edit: That method is there since 15.0, so i'd check the content of you classpath too.

编辑:该方法自 15.0 以来就存在,因此我也会检查您的类路径的内容。

回答by cн?dk

Referring to NoSuchMethodError Oracle Documentation:

参考NoSuchMethodError Oracle 文档

The NoSuchMethodError:is thrown if an application tries to call a specified method of a class (either static or instance), and that class no longer has a definition of that method.

所述NoSuchMethodError:如果应用程序试图调用类(静态或实例)的指定方法被抛出,而该类已不再具有该方法的定义。

Normally, this error is caught by the compiler; this error can only occur at run time if the definition of a class has incompatibly changed.

正常情况下,这个错误会被编译器捕获;如果类的定义发生了不兼容的更改,则此错误只会在运行时发生

I think you got this Exception because you have more than one version of this jar in your classpath, and since the createStarted()method is available from the 15.0version I whould say that you have an other old version of it, probably due to a dependency problem.

我认为你得到这个异常是因为你的类路径中有多个版本的这个 jar,并且由于该createStarted()方法可以从15.0版本中获得,我可以说你有它的另一个旧版本,可能是由于依赖问题。

回答by jai

Moving to the latest version of Guava did it for me

迁移到最新版本的 Guava 对我来说

回答by Uncle Long Hair

In my case one of my Maven dependencies was picking up a newer version of Guava (16.0.1) that apparently does not have this method. When I added an exclusion to that dependency in my pom.xml, instead an older (correct) version of Guava was picked up by another of my dependencies and then it worked.

在我的情况下,我的 Maven 依赖项之一是选择较新版本的 Guava (16.0.1),它显然没有这种方法。当我在我的pom.xml.

You can find this by printing your dependency tree via mvn dependency:treeand then looking at what is picking up the newer version of guava. You might need to add more than one exclusion to get it right.

你可以通过打印你的依赖树来找到这个mvn dependency:tree,然后查看是什么选择了新版本的番石榴。您可能需要添加多个排除项才能使其正确。