带有 Java 和 gradle 的控制台应用程序 [英] Console application with Java and gradle
问题描述
我正在用 Java 和 gradle 编写一个控制台应用程序.我正在使用 application
插件 并在 <代码>build.gradle.
I am writing a console application with Java and gradle. I am using the application
plugin and have the required fields correctly configured in build.gradle
.
在我的主类中,我将 BufferedReader
与 System.in
链接起来.问题是:当我在项目目录中运行 gradle run
时,阅读器不会等待我的控制台输入.BufferedReader#readLine
在第一次调用时返回 null
.这种行为对于我正在做的事情来说是不可取的.
In my main class I have BufferedReader
linked with System.in
. Here's the problem: When I run gradle run
in project directory, the reader does not wait for my console input. BufferedReader#readLine
instead returns null
on the very first call. This behavior is not desirable for what am I doing.
解决办法是什么?是否有单独的 gradle 控制台应用程序插件,或者我是否需要以某种方式调整 application
插件以满足我的需求?
What is the solution? Is there a separate console application plugin for gradle or do I need to tweak application
plugin somehow to suit my needs?
推荐答案
默认情况下,gradle 构建的 system.in 未与运行 (JavaExec) 任务的 system.in 连接.您可以执行以下操作:
Per default the system.in of your gradle build is not wired up with the system.in of the run (JavaExec) task. You can do the following:
// build.gradle (Groovy syntax)
run {
standardInput = System.in
}
// build.gradle.kts (Kotlin syntax)
tasks.named<JavaExec>("run") {
standardInput = System.`in`
}
这篇关于带有 Java 和 gradle 的控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!