Search code examples
gradlebuild.gradlegradle-kotlin-dslsystem.in

Gradle not waiting for scanner using System.in


I'm trying to run my project with gradle Kotlin 8.2 and instead of waiting for a user input on scanner it simply gives me this error "Exception in thread "main" java.util.NoSuchElementException: No line found"

This is the code in my main:

Scanner scanner = new Scanner(System.in);

System.out.println("What would you like to do?\n==========================\n0) Quit\n1) Show all Tree Species\n2) Show tree species of conservation status\n3)Show all locations\n4)Show locations with forest coverage and/or rainfall");
System.out.print("Choice (0-4): ");
String choice = scanner.nextLine();

System.out.println("You chose " + choice);

And this is my build.gradle.kts file:

plugins {
    id("java")
    id("application")
}

application {
    mainClass.set("programming3.StartApplication")
}

group = ".programming3"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    testImplementation(platform("org.junit:junit-bom:5.9.1"))
    testImplementation("org.junit.jupiter:junit-jupiter")
    implementation("com.google.code.gson:gson:2.10.1")
}

tasks.test {
    useJUnitPlatform()
}

I tried adding this to my build.gradle file but it did nothing to help

tasks {
    run {
        System.`in`
    }
}

I also searched online and all solutions seem to not be valid in my current version of gradle.

I found the solution, need to add this into the build.gradle file for those having the same issue:

tasks.getByName("run", JavaExec::class) {
 standardInput = System.`in`
}

Solution

  • Here is the solution I found to add to the build.gradle file

    tasks.getByName("run", JavaExec::class) {
     standardInput = System.`in`
    }