ReadAfterEOFException при запуске проекта Kotlin Multiplatform на JVM
Я пытаюсь запустить свой проект Kotlin Multiplatform на JVM. Но когда программа вызывает readln()
, она вылетает:
Exception in thread "main" kotlin.io.ReadAfterEOFException: EOF has already been reached
at kotlin.io.ConsoleKt.readln(Console.kt:152)
at ru.morozovit.dncryptor101.MainKt.main(Main.kt:32)
at ru.morozovit.dncryptor101.MainKt.main(Main.kt)
Я видел это решение, но оно для обычного Kotlin-проекта. Есть ли аналогичное решение для Kotlin Multiplatform?
build.gradle.kts
plugins {
kotlin("multiplatform") version "2.1.0"
id("com.google.osdetector") version "1.7.3"
}
group = "ru.morozovit"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
kotlin {
jvm()
js().browser()
linuxX64()
linuxArm64()
sourceSets {
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
val jvmMain by getting {
dependencies {
// As JavaFX have platform-specific dependencies, we need to add them manually
val fxSuffix = when (osdetector.classifier) {
"linux-x86_64" -> "linux"
"linux-aarch_64" -> "linux-aarch64"
"windows-x86_64" -> "win"
"osx-x86_64" -> "mac"
"osx-aarch_64" -> "mac-aarch64"
else -> throw IllegalStateException("Unknown OS: ${osdetector.classifier}")
}
// Replace "compileOnly" with "implementation" for a non-library project
implementation("org.openjfx:javafx-base:18.0.2:${fxSuffix}")
implementation("org.openjfx:javafx-graphics:18.0.2:${fxSuffix}")
implementation("org.openjfx:javafx-controls:18.0.2:${fxSuffix}")
}
}
}
}
settings.gradle.kts
rootProject.name = "dncryptor101"
Main.kt
package ru.morozovit.dncryptor101
import ru.morozovit.dncryptor101.base.Solver
import ru.morozovit.dncryptor101.base.Solver.Companion.ALPHABET_RUSSIAN
import kotlin.random.Random
import kotlin.random.nextInt
fun main() {
val version = "1.2"
val solver = Solver()
println(
"""
Dncryptor101
Version $version
------------------
""".trimIndent()
)
while (true) {
print(
"""
|Select an option:
|0. Exit
|1. Decrypt text
|2. Encrypt text
|3. About${if (!isNative) "\n4. Enter GUI" else ""}
|Type a number:
""".trimMargin()
)
val choice: Int
try {
choice = readln().toInt() // вот здесь
} catch (_: NumberFormatException) {
println("Try again.")
continue
}
when (choice) {
0 -> break
1 -> {
print("Enter a word to decrypt: ")
val inp = readln()
print("Shift (0/Enter if you don't know (list all variants)): ")
var shift: Int
while (true) {
try {
val inp1 = readln()
shift = if (inp1 == "") 0 else inp1.toInt()
break
} catch (e: NumberFormatException) {
println("Your input is not a number. Try again.")
continue
}
}
if (shift != 0) {
val word = solver.decrypt(inp, shift)
println("Your decrypted word: $word")
break
}
println("\nPossible variants:")
val words = solver.decrypt(inp)
var col = 1
for (word in words) {
print(word)
col++
if (col == 3 + 1 || word.length >= 20) {
col = 1
println()
} else {
print(" ")
}
}
break
}
2 -> {
print("Type a word to encrypt: ")
val inp = readln()
print("Shift (0 to generate a random one): ")
var shift: Int
while (true) {
try {
shift = readln().toInt()
break
} catch (_: NumberFormatException) {
println("Your input is not a number. Try again.")
continue
}
}
if (shift == 0) {
shift = Random.nextInt(
-ALPHABET_RUSSIAN.length..ALPHABET_RUSSIAN.length
)
println("Your shift: $shift")
}
val word = solver.encrypt(inp, shift)
println("Your encrypted word: $word")
break
}
3 -> {
println(
"""
A simple program to encrypt and decrypt text.
Version $version
""".trimIndent()
)
break
}
4 -> {
if (isNative) {
println("GUI is available only for Java.")
break
}
launchGUI()
break
}
else -> {
println("Unknown choice.")
}
}
}
}
Кстати, когда я выполняю gradlew --info runJvm
, копирую команду, через которую Gradle запускает мою программу и запускаю ее, ошибка исчезает.