input - Kotlin and Gradle - Reading from stdio -
i trying execute kotlin class using command:
./gradlew -q run < src/main/kotlin/samples/input.txt
here helloworld.kt class:
package samples fun main(args: array<string>) { println("hello, world!") val lineread = readline() println(lineread) }
here build.gradle.kts:
plugins { kotlin("jvm") application } application { mainclassname = "samples.helloworldkt" } dependencies { compile(kotlin("stdlib")) } repositories { jcenter() }
the code executes, data contained inside input.txt file not displayed. here output get:
hello, world! null
i want able execute gradlew command above , input.txt stream redirected stdio. can in c++. once compile .cpp file, can run:
./my_code < input.txt
and executes expected.
how can achieve same thing kotlin , gradle?
update: based on this answer, i've tried adding build.gradle.kts not valid syntax:
almost, doesn't work :'(
in theory
my understanding: < input.txt
sets standard input gradlew process, default not forwarded program.
you want add build.gradle.kts:
run { standardinput = system.`in` }
sources:
https://discuss.gradle.org/t/why-doesnt-system-in-read-block-when-im-using-gradle/3308/2
https://discuss.gradle.org/t/how-can-i-execute-a-java-application-that-asks-for-user-input/3264
in practice
these build configs same me, yet groovy works , kotlin doesn't. i'm starting think gradle kotlin dsl doesn't support standardinput
term yet :/
here's working groovy version if that's help:
apply plugin: 'kotlin' apply plugin: 'application' buildscript { ext.kotlin_version = '1.1.4' repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } repositories { jcenter() } dependencies { // api => exported consumers (found on compile classpath) // implementation => used internally (not exposed consumers) implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" } mainclassname = "samples.helloworldkt" run { standardinput = system.in }
Comments
Post a Comment