Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoSuchMethodError: java.nio.ByteBuffer.limit(I)Ljava/nio/ByteBuffer #3244

Closed
sbordet opened this issue Jan 8, 2019 · 10 comments
Closed

NoSuchMethodError: java.nio.ByteBuffer.limit(I)Ljava/nio/ByteBuffer #3244

sbordet opened this issue Jan 8, 2019 · 10 comments
Labels
Bug For general bugs on Jetty side Build Help Wanted

Comments

@sbordet
Copy link
Contributor

sbordet commented Jan 8, 2019

Like many other projects (Tomcat, MongoDB, etc.) compiling with JDK 9+ with release 8 produces incorrect bytecode for these ByteBuffer methods:

position(int)
limit(int)
mark​()
reset​()
clear()
flip()
rewind​()

Running a Jetty compiled with JDK 9+ with release 8 in a JDK 8 JVM will produce a NoSuchMethodError.

The solution is to cast the ByteBuffer to Buffer when calling those methods:

((Buffer)byteBuffer).position(0);
@joakime
Copy link
Contributor

joakime commented Jan 8, 2019

To be clear this is because Java 9 introduced a Covariant return type that when compiled with Java 9+ includes the return type in the (jetty) bytecode, rendering the bytecode incompatible with Java 8.
Right?

@joakime joakime added Bug For general bugs on Jetty side Build Java 11 labels Jan 8, 2019
@sbordet
Copy link
Contributor Author

sbordet commented Jan 8, 2019

@joakime correct.

The call byteBuffer.position(0) in JDK 8 calls method Buffer.position(I)LBuffer while in JDK 9+ calls method ByteBuffer.position(I)LByteBuffer.
When a JVM 8 sees the latter it does not find it, hence the error.

I'm still astonished that JDK 9's javac produces the wrong bytecode even when specifying --release 8.

@gregw
Copy link
Contributor

gregw commented Jan 9, 2019

REALLY??? ugh!
But should be a simple fix if we have all been using BufferUtil like we should be :)

However, we don't want to "fix" this in jetty-10 as that is never intended to be run on java8, so do we just need to fix this in jetty-9?

@sbordet
Copy link
Contributor Author

sbordet commented Jan 10, 2019

@gregw, correct we only need it in 9.4.x.

@DreamWuGit
Copy link

I also run into this problem, any update upon this ?

@joakime
Copy link
Contributor

joakime commented Mar 6, 2019

@DreamWuGit to work around, in your project, compile with JDK8, run with any JDK.

@sbordet
Copy link
Contributor Author

sbordet commented Mar 6, 2019

@joakime but the problem is that Maven Central artifacts that we produce are compiled with JDK 9+.

@stuart-marks
Copy link

Sorry to butt in here, but I see this and several other projects adding unnecessary casts for this issue. I have never seen javac produce the wrong bytecodes when given the proper --release flag. The wrong bytecodes issue seems to occur when one is using (for example) JDK 9 and using "javac -source 8 -target 8", which does let Java 9 library dependencies leak into Java 8 classes (and which issues a warning) and which results in this problem. Usually the problem seems to be that something in the build system is still using the -source 8 -target 8 technique and thus the problem persists.

Now if javac --release 8 is still letting Java 9 dependencies into the Java 8 class files, that's a bug and we should fix it.

@sbordet
Copy link
Contributor Author

sbordet commented May 23, 2019

@stuart-marks I just double checked this and you are right, --release 8 produces the right bytecode.

We do have a profile that is automatically enabled when compiling with JDK > 8 (https://github.com/eclipse/jetty.project/blob/jetty-9.4.18.v20190429/pom.xml#L1843) that enforces --release 8 so we should be good.

@coleHafner
Copy link

Thank you, @sbordet. I'm new to Java and your comment helped me fix the issue quickly!

rajyengi pushed a commit to facebook/buck that referenced this issue Jan 12, 2021
* Update JNA to resolve framework loading issue on Big Sur

java-native-access/jna#1215

* Remove workaround initializing JNA early

java-native-access/jna#652

This issue was resolved and property SIZE does not exist with JNA version 5.6.0.

* Update NuProcess to 2.0.1 for compatibility with JNA

* Build NuProcess with openjdk version "1.8.0_275"

jetty/jetty.project#3244

* Cherry pick a2912b9

a2912b9
anovstrup added a commit to anovstrup/dsiutils that referenced this issue Jan 14, 2021
When compiled with Java 9+, even when the compilation target is Java 8 ByteBuffer.position calls result in a `java.lang.NoSuchMethodError: java.nio.ByteBuffer.position(I)Ljava/nio/ByteBuffer;` error when running in a Java 8 JVM (see [this issue](jetty/jetty.project#3244)). Casting to java.nio.Buffer before calling position avoids the error.
Bencodes pushed a commit to lyft/buck that referenced this issue Feb 10, 2021
* Update JNA to resolve framework loading issue on Big Sur

java-native-access/jna#1215

* Remove workaround initializing JNA early

java-native-access/jna#652

This issue was resolved and property SIZE does not exist with JNA version 5.6.0.

* Update NuProcess to 2.0.1 for compatibility with JNA

* Build NuProcess with openjdk version "1.8.0_275"

jetty/jetty.project#3244

* Cherry pick a2912b9

facebook@a2912b9
jazdw added a commit to MangoAutomation/ma-core-public that referenced this issue Mar 1, 2021
artembilan added a commit to artembilan/spring-integration that referenced this issue Apr 9, 2021
When we build with Java 9+ and target for Java 8,
we get an incompatible bytecode around these method for `ByteBuffer`:

```
position(int)
limit(int)
mark()
reset()
clear()
flip()
rewind()
```

The recommendation is to cast to `Buffer` when we call these methods

* Fix all the production code using `ByteBuffer` for recommended cast to `Buffer`

Related to https://build.spring.io/browse/INTEXT-AWS-306

See more info in this Jetty issue: jetty/jetty.project#3244
garyrussell pushed a commit to spring-projects/spring-integration that referenced this issue Apr 9, 2021
When we build with Java 9+ and target for Java 8,
we get an incompatible bytecode around these method for `ByteBuffer`:

```
position(int)
limit(int)
mark()
reset()
clear()
flip()
rewind()
```

The recommendation is to cast to `Buffer` when we call these methods

* Fix all the production code using `ByteBuffer` for recommended cast to `Buffer`

Related to https://build.spring.io/browse/INTEXT-AWS-306

See more info in this Jetty issue: jetty/jetty.project#3244
dwursteisen added a commit to dwursteisen/minigdx-glft-parser that referenced this issue Apr 26, 2021
shepting pushed a commit to airbnb/buck that referenced this issue Nov 11, 2021
* Update JNA to resolve framework loading issue on Big Sur

java-native-access/jna#1215

* Remove workaround initializing JNA early

java-native-access/jna#652

This issue was resolved and property SIZE does not exist with JNA version 5.6.0.

* Update NuProcess to 2.0.1 for compatibility with JNA

* Build NuProcess with openjdk version "1.8.0_275"

jetty/jetty.project#3244

* Cherry pick a2912b9

facebook@a2912b9
shepting pushed a commit to airbnb/buck that referenced this issue Nov 11, 2021
* Update JNA to resolve framework loading issue on Big Sur

java-native-access/jna#1215

* Remove workaround initializing JNA early

java-native-access/jna#652

This issue was resolved and property SIZE does not exist with JNA version 5.6.0.

* Update NuProcess to 2.0.1 for compatibility with JNA

* Build NuProcess with openjdk version "1.8.0_275"

jetty/jetty.project#3244

* Cherry pick a2912b9

facebook@a2912b9
shepting pushed a commit to airbnb/buck that referenced this issue Nov 12, 2021
* Update JNA to resolve framework loading issue on Big Sur

java-native-access/jna#1215

* Remove workaround initializing JNA early

java-native-access/jna#652

This issue was resolved and property SIZE does not exist with JNA version 5.6.0.

* Update NuProcess to 2.0.1 for compatibility with JNA

* Build NuProcess with openjdk version "1.8.0_275"

jetty/jetty.project#3244

* Cherry pick a2912b9

facebook@a2912b9
kevincai added a commit to kevincai/starrocks that referenced this issue Apr 18, 2024
* make spark ddp works correctly when compiling by jdk9+ and running on
  jdk8
* refer to: jetty/jetty.project#3244
* partially backport StarRocks#33738
* upgrade maven-compiler-plugin to 3.13.0 so it can automatically ignore
  the release configuration when build with jdk8

Signed-off-by: Kevin Xiaohua Cai <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug For general bugs on Jetty side Build Help Wanted
Projects
None yet
Development

No branches or pull requests

6 participants