Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.nio.Buffer not loading clear() method on runtime [duplicate]

Tags:

java

buffer

nio

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?

like image 446
BobbyMacBob Avatar asked Feb 08 '18 19:02

BobbyMacBob


1 Answers

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());
like image 174
BobbyMacBob Avatar answered Oct 01 '22 07:10

BobbyMacBob