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

6

7

My app is throwing NoSuchMethodError: com.google.common.base.Stopwatch.createStarted()Lcom/google/common/base/Stopwatch error. 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?

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:

  <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:

[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$1.load(PeriodicFirewallCounterRule.java:86)
[INFO]  at org.restlet.ext.apispark.internal.firewall.rule.PeriodicFirewallCounterRule$1.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
Sipple answered 4/3, 2015 at 13:20 Comment(2)
Looks like you have incompatible jars in your classpath.Macrospore
Similar issue with selenium 2.54.0 , they are using guava but there is a mismatch in guava version 14Lysippus
E
5

When using the extension org.restlet.ext.apispark, the guava dependency retrieved has the version 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:

<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...

Hope that helps you, Thierry

Ellynellynn answered 5/3, 2015 at 8:59 Comment(9)
You are using org.restlet.jse how about org.restlet.gae would it be the same?Sipple
Also I have excluded the other guava dependency, still APISpark library would not work with GAE here is a sample error log: pastekit.com/jRZAyYSipple
It's work for me on the Java GAE development server... Does it work for you at this level? Or is it only on the GAE platform itself?Ellynellynn
I did try a "work around" relating to the error I pasted here pastekit.com/jRZAyY now I am getting this error: pastekit.com/joBAW I hope you can checkSipple
I tried your maven configuration within my test project and it works on my side in a GAE dev server. I use GAE 1.9.18. Could you provide me a small project (in a github repository for example) so I can reproduce your problem and debug it? Thanks!Ellynellynn
I have fixed the problem with your tip (see my answer below) however the next issue is that the Firewall does not seem to work as a "firewall" I mean, even the resource is accessed simultaneously it does not throw "Too Many Request" error as expectedSipple
Let us continue this discussion in chat.Ellynellynn
Pleased to hear that for your fix! I internally created an issue for the second part of your comment. I'll keep you posted here...Ellynellynn
Thanks, yes no more error, I created a separate question for the new issue: #28900355Sipple
S
2

This is the solution that fixed the error:

First exclude old Guava dependency then:

  <dependency>
      <groupId>org.restlet.gae</groupId>
      <artifactId>org.restlet.ext.apispark</artifactId>
      <version>${version.restlet}</version>
      <exclusions>
          <exclusion>
              <groupId>com.fasterxml.jackson.core</groupId>
              <artifactId>jackson-databind</artifactId>
          </exclusion>
      </exclusions>
  </dependency>
  <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.5.1</version>
  </dependency>
Sipple answered 6/3, 2015 at 12:51 Comment(0)
F
1

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:

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.

Flannelette answered 4/3, 2015 at 13:29 Comment(0)
B
1

Referring to NoSuchMethodError Oracle Documentation :

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.

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.0 version I whould say that you have an other old version of it, probably due to a dependency problem.

Brig answered 4/3, 2015 at 13:48 Comment(2)
Yes, I am seeing now from the target WEB-INF/lib folder two guava one is 14 and one is 16.0.1 how oddSipple
That's the problem! the 14 is the one causing this Exception.Condemnation
H
0

Moving to the latest version of Guava did it for me

Holstein answered 3/9, 2016 at 20:19 Comment(1)
what is the latest version you are using?Vedette
A
0

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.

You can find this by printing your dependency tree via mvn dependency:tree and 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.

Ammoniacal answered 5/4, 2018 at 18:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.