13

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?

1
  • 4
    Check this comment. It appears that Java9 altered some of the Buffer's method signatures. Commented Feb 8, 2018 at 20:03

2 Answers 2

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());
Sign up to request clarification or add additional context in comments.

2 Comments

It doesn't have to be cast. Just recompiled. Naughty Oracle.
When you want to be compatible with Java 8, just use the --release option.
3

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();
  }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.