12

So I am a developer for a project that uses a java agent to inject. It should be noted though that this error occurs after main is called.

Everything is going fine for most users, but a few are having an issue where java.nio.IntBuffer isn't loading clear() (inherited from Buffer)

Error:

java.lang.NoSuchMethodError: java.nio.IntBuffer.clear()Ljava/nio/IntBuffer;

Then the stacktrace, which simply gives the first time clear() is called in our code.

What is the cause of this (besides the fact that Java isn't loading at runtime) and how do I fix it?

Alex Pawelko
  • 414
  • 1
  • 4
  • 14
  • 4
    Check this [comment](https://github.com/plasma-umass/doppio/issues/497#issuecomment-334740243). It appears that Java9 altered some of the Buffer's method signatures. – Janez Kuhar Feb 08 '18 at 20:03

2 Answers2

13

Thanks to the comment from Janez Kuhar and doing some digging this is caused by JDK9 breaking compatibility. In our code we call IntBuffer.clear() and expect it to return Buffer, but in JDK9 they made all Buffer methods return the child type (i.e. ByteBuffer or IntBuffer as opposed to just Buffer), therefore people running JRE8 (most of our userbase) experience NoSuchMethodError's because the return type is incompatible and must be casted like this

someMethod(((Buffer)intBuffer).clear());
Alex Pawelko
  • 414
  • 1
  • 4
  • 14
2

Here is a nice explanation of the NoSuchMethodError.

https://www.codenong.com/js8f219d981aa9/

Note: Also the flip() method has this problem.

If you are compiling with jdk9+ and running on jdk8 you need to cast. In my project I created 2 utility methods for this problem:

  public static void clear(Buffer buffer)
  {
      buffer.clear();
  }

  public static void flip(Buffer buffer)
  {
      buffer.flip();
  }
Kees Kuip
  • 71
  • 1
  • 2