protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Java: NoSuchMethodError on JRE 8

Open minwoox opened this issue 2 years ago • 1 comments

What version of protobuf and what language are you using? Version: protobuf-java-3.21.7 (there were no problems with 3.21.1) Language: Java

What operating system (Linux, Windows, ...) and version? Linux

What runtime / compiler are you using (e.g., python version or gcc version) Build JDK 9+ and run with JRE 8

What did you do? Build Protobuf with JDK 9+ and run with JRE 8. Because CodedOutputStream uses ByteBuffer.position(int) that overrides the method in the superclass Buffer from JDK9+, it produces NoSuchMethodError when it's run with JRE 8. as follow:

    java.lang.NoSuchMethodError: java.nio.ByteBuffer.position(I)Ljava/nio/ByteBuffer;
        at com.google.protobuf.CodedOutputStream$UnsafeDirectNioEncoder.writeStringNoTag(CodedOutputStream.java:2153)
        at com.google.protobuf.CodedOutputStream$UnsafeDirectNioEncoder.writeString(CodedOutputStream.java:1922)
        at com.google.protobuf.GeneratedMessageV3.writeString(GeneratedMessageV3.java:3195)
        at com.linecorp.armeria.grpc.testing.Messages$SimpleResponse.writeTo(Messages.java:4363)
        at com.linecorp.armeria.internal.common.grpc.GrpcMessageMarshaller.serializeProto(GrpcMessageMarshaller.java:202)
        at com.linecorp.armeria.internal.common.grpc.GrpcMessageMarshaller.serializeResponse(GrpcMessageMarshaller.java:145)
        at com.linecorp.armeria.internal.common.grpc.GrpcMessageMarshallerTest.serializeResponse(GrpcMessageMarshallerTest.java:121)

This is a well-described article: https://www.morling.dev/blog/bytebuffer-and-the-dreaded-nosuchmethoderror/

Please note that there were any problems with 3.21.2 version.

What did you expect to see Just work fine.

What did you see instead? NoSuchMethodError

minwoox avatar Dec 22 '22 03:12 minwoox

@deannagarcia the linked article shows a way to fix this at the build system layer that would work for us

fowles avatar Dec 22 '22 15:12 fowles

I'm also seeing this same issue in 3.21.10. Here's my stack trace:

 java.lang.NoSuchMethodError: java.nio.CharBuffer.flip()Ljava/nio/CharBuffer;
        at com.google.protobuf.TextFormat$Parser.toStringBuilder(TextFormat.java:1720)
        at com.google.protobuf.TextFormat$Parser.merge(TextFormat.java:1704)
        at com.google.protobuf.TextFormat$Parser.merge(TextFormat.java:1676)

FYI, 3.21.9 doesn't have the issue.

kimoonkim avatar Jan 30 '23 22:01 kimoonkim

we're trying to upgrade from 3.21.9 to 3.21.12 and are now hitting the same problem as in the ticket description:

Caused By (java.lang.NoSuchMethodError) java.nio.ByteBuffer.position(I)Ljava/nio/ByteBuffer;
    com.google.protobuf.IterableByteBufferInputStream.read():143
    com.google.protobuf.CodedInputStream$StreamDecoder.read():2080
    com.google.protobuf.CodedInputStream$StreamDecoder.tryRefillBuffer():2831
    com.google.protobuf.CodedInputStream$StreamDecoder.isAtEnd():2754
    com.google.protobuf.CodedInputStream$StreamDecoder.readTag():2107
(...)

we compile with jdk11 but target java 8 (runtime) but from my understanding the problem is how the protobuf release artifacts were compiled (i.e. missing -release 8 flag?)

edit: more testing suggests the problem got introduced between 3.21.9 and 3.21.10

XN137 avatar Feb 01 '23 17:02 XN137

@fowles @deannagarcia i apologize for the ping but isnt this a somewhat serious problem? it cant have been intentional to break java 8 compatibility between patch versions

XN137 avatar Feb 17 '23 12:02 XN137

I'm so sorry I missed this, thank you for the ping. It was definitely not intentional to break java 8 compatibility between patch versions.

We just released 22.0, can you see if that version works for you with java 8?

I'm currently talking to the team about getting a patch for 21 out with this fix, but want to see if one will be needed for 22 as well.

deannagarcia avatar Feb 17 '23 16:02 deannagarcia

@deannagarcia thanks for the quick response, before my ping I had tried 3.22.0 but i was still hitting the same problem in our build

XN137 avatar Feb 17 '23 16:02 XN137

Any updates on this (still open) issue?

gashuach avatar Jul 25 '23 14:07 gashuach

This issue has been fixed (commit d40aadf823cf7e1e62b65561656f689da8969463) and patched to all supported branches.

googleberg avatar Jul 25 '23 15:07 googleberg