I've received this error: `java.lang.NoSuchMethodE...
# ktor
с
I've received this error:
java.lang.NoSuchMethodError: java.nio.ByteBuffer.position(I)Ljava/nio/ByteBuffer;
After trying to make a request to this endpoint:
Copy code
post("/home") {
    call.sessions.set(UserSession(id = "123", name = "Name"))
    call.respondRedirect(Endpoint.Authorized.path)
}
I'm using 2.0.0-beta-1 version.
e
Hey, could you tell me the version you’re using?
с
Copy code
2.0.0-beta-1
That 'call.sessions.set()' is creating this error. Even though I've setup a Session and Authentication plugin successfully.
e
And could you also tell me what version you’re using?
с
@e5l 2.0.0-beta-1
h
I guess, Leonid means JDK version too, because java.nio.ByteBuffer is a JVM method
с
Screenshot_1.png
h
Which engine? But that's strange, java.nio.ByteBuffer.position(I)Ljava/nio/ByteBuffer; aka java.nio.Buffer.position(I)Ljava/nio/Buffer is available since Java 1.4.
с
Netty, engine main
I had the same project but only with a different Ktor version (1.6.7) and everything seems to work just fine.
@e5l @hfhbd When I try to downgrade Ktor version from 2.0.0-beta-1 to 1.6.7 in my gradle.properties file, I'm getting this error when running the server. How I'm supposed to downgrade the Ktor version then? The question may sound stupid, but I'm really not sure how to do that...
j
In Java 9 the return type of various methods on ByteBuffer was changed to be covariant
So if you are compiling with JDK 9+ but running on 8 you'll see that exception, even with source compatibility set. You need to set the release flag which changes the bootclasspath (the built in APIs) to the old signature if you plan on running on an older JDK than you compile with.
h
Or Ktor needs to be build with Java 8?
j
There were a bunch of articles written a few years ago about it. Here's the first one I found searching: https://saker.build/blog/javac_source_target_parameters/index.html but there's many more. I vaguely remember one from maybe Stuart Marks from Oracle too
No you never need to build with an old JDK
h
And even this "simple" function (position(Int) is affected, because it returns ByteBuffer to allow chaining?
j
Yes
h
Thanks!
с
I've just upgraded to a version 11, and it works. Thanks guys 👍
272 Views