0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ktorのHttpClientでCaused by: java.lang.NoClassDefFoundError: kotlinx/io/unsafe/UnsafeBufferOperations

Posted at

はじめに

KtorでHttpリクエストをするのに手間取ったのでまとめます

問題

以下のドキュメントどおりに設定を進めたところエラーが出ました

    suspend fun get(): Res {

        val client = HttpClient(CIO)
        val response: HttpResponse = client.request("https://ktor.io/") {
            method = HttpMethod.Get
        }
java.io.IOException: kotlinx/io/unsafe/UnsafeBufferOperations
	at io.ktor.utils.io.CloseToken.<init>(CloseToken.kt:27)
	at io.ktor.utils.io.ByteChannel.cancel(ByteChannel.kt:133)
	at io.ktor.utils.io.ByteWriteChannelOperationsKt$writer$job$1.invokeSuspend(ByteWriteChannelOperations.kt:151)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:101)
	at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:113)
	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:89)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:589)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:823)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:720)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:707)
Caused by: java.lang.NoClassDefFoundError: kotlinx/io/unsafe/UnsafeBufferOperations
	at io.ktor.utils.io.ByteWriteChannelOperations_jvmKt.write(ByteWriteChannelOperations.jvm.kt:26)
	at io.ktor.utils.io.ByteWriteChannelOperations_jvmKt.write$default(ByteWriteChannelOperations.jvm.kt:24)
	at io.ktor.network.sockets.CIOReaderKt.readFrom(CIOReader.kt:134)
	at io.ktor.network.sockets.CIOReaderKt.access$readFrom(CIOReader.kt:1)
	at io.ktor.network.sockets.CIOReaderKt$attachForReadingDirectImpl$1.invokeSuspend(CIOReader.kt:97)
	at io.ktor.network.sockets.CIOReaderKt$attachForReadingDirectImpl$1.invoke(CIOReader.kt)
	at io.ktor.network.sockets.CIOReaderKt$attachForReadingDirectImpl$1.invoke(CIOReader.kt)
	at io.ktor.utils.io.ByteWriteChannelOperationsKt$writer$job$1.invokeSuspend(ByteWriteChannelOperations.kt:143)
	... 8 common frames omitted

解決方法

おそらくマルチプラットフォームの関係で入れているライブラリが違うようです

image.png

調べたところ該当のクラスはありませんでした
ライブラリを検索したところ以下のリポジトリがヒットしてこちらをktor-client-core-jvmの代わりに入れました

<dependency>
    <groupId>org.jetbrains.kotlinx</groupId>
    <artifactId>kotlinx-io-core-jvm</artifactId>
    <version>0.6.0</version>
</dependency>

うまくいきました

image.png

おわりに

ドキュメント通りでは絶対にうまく行かないと思ったのでさんこうになるとうれしいで

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?