[英]How to start an interactive java app with gradle
I want to write a gradle task that runs me this small application: 我想编写一个gradle任务来运行这个小应用程序:
import java.lang.IllegalArgumentException
class TestApp{
companion object {
@JvmStatic
fun main(args:Array<String>){
val a = try{
args[0].toInt()
}catch (e:Exception) {throw argException}
val b = try{
args[1].toInt()
}catch (e:Exception) {throw argException}
print("$a + $b = ")
val answer = readLine()!!.toInt()
println(if(a+b == answer)"CORRECT" else "WRONG!")
}
private val argException:IllegalArgumentException by lazy { IllegalArgumentException("expecting two integers as args") }
}
}
If I run the application with, say, Intellij, the app will pause at the readline()
and expect user input. 如果我使用Intellij运行应用程序,则该应用程序将在
readline()
处暂停并等待用户输入。
However, if I add a gradle task for it 但是,如果我为其添加了gradle任务
task runTestApp(type:JavaExec){
main = "${javaMainTestApp}"
classpath = sourceSets.main.runtimeClasspath
}
and run, say, 然后说
gradle runTestApp --args="2 4"
Then I get 然后我得到
2 + 4 = Exception in thread "main" kotlin.KotlinNullPointerException
at [...].app.TestApp$Companion.main(TestApp.kt:19)
at [...].app.TestApp.main(TestApp.kt)
Why is that? 这是为什么? And more importantly, how do I get the execution to wait for user input?
更重要的是,如何获取执行以等待用户输入?
UPDATE UPDATE
Thanks @tim_yates: 感谢@tim_yates:
adding standardInput = System.in
makes the app accept user input 添加
standardInput = System.in
使应用程序接受用户输入
but results in an output like: 但结果如下:
3 + 5 =
<<==========---> 80% EXECUTING [20s]
> :runTestApp
8
where 8 is the user input. 其中8是用户输入。
consequently, when the app finishes, the output reads 因此,当应用程序完成时,输出为
3 + 5 =
<<======CORRECT> 80% EXECUTING [22s]
Maybe you could use the application plugin and do a gradle run
. 也许您可以使用应用程序插件并执行
gradle run
。 Or you could use the distribution plugin and run the script. 或者,您可以使用分发插件并运行脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.