带有 Java 和 gradle 的控制台应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13172137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:45:15  来源:igfitidea点击:

Console application with Java and gradle

javaconsole-applicationgradle

提问by missingfaktor

I am writing a console application with Java and gradle. I am using the applicationpluginand have the required fields correctly configured in build.gradle.

我正在用 Java 和 gradle 编写一个控制台应用程序。我正在使用该application插件并在build.gradle.

In my main class I have BufferedReaderlinked with System.in. Here's the problem: When I run gradle runin project directory, the reader does not wait for my console input. BufferedReader#readLineinstead returns nullon the very first call. This behavior is not desirable for what am I doing.

在我的主要课程中,我BufferedReaderSystem.in. 问题是:当我gradle run在项目目录中运行时,阅读器不会等待我的控制台输入。BufferedReader#readLine而是null在第一次调用时返回。这种行为对于我在做什么是不可取的。

What is the solution? Is there a separate console application plugin for gradle or do I need to tweak applicationplugin somehow to suit my needs?

解决办法是什么?是否有单独的 gradle 控制台应用程序插件,或者我是否需要以application某种方式调整插件以满足我的需求?

回答by Rene Groeschke

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:

默认情况下,您的 gradle 构建的 system.in 未与 run (JavaExec) 任务的 system.in 连接。您可以执行以下操作:

run{
    standardInput = System.in
}

回答by loeschg

As stated above, add

如上所述,添加

run {
   standardInput = System.in
}

And run:

并运行:

gradle console:run -q --console=plain

where:

在哪里:

  • -qruns task in "quiet" mode (to avoid having > Building > :run)
  • --console=plaindrops execution status: <=-> 80% EXECUTING [TIME]
  • -q在“安静”模式下运行任务(以避免有> Building > :run
  • --console=plain丢弃执行状态: <=-> 80% EXECUTING [TIME]

Source: https://docs.gradle.org/current/userguide/gradle_command_line.html

来源:https: //docs.gradle.org/current/userguide/gradle_command_line.html

回答by CaTalyst.X

Chances are, the problem lies in your java code. All the application plugin does is compile the java code, and run the main class that you specify. Can you post the code in your main class that you specified for the application plugin (mainClassName) ?

很有可能,问题出在您的 Java 代码中。应用程序插件所做的就是编译 java 代码,并运行您指定的主类。您可以在为应用程序插件 (mainClassName) 指定的主类中发布代码吗?