无法让 Gradle 使用 System.in
Can't get Gradle to use System.in
This question 解释了在 运行 将任务 运行 项目中的特定 class 时如何使用 System.in
。
但目前对我来说它不起作用:尽管我在 build.gradle
中包含了 application
插件和以下行:
mainClassName = "misc.StreamsExp"
run{
standardInput = System.in
}
task stream( type: JavaExec, dependsOn: assemble ){
classpath sourceSets.main.runtimeClasspath
main = "misc.StreamsExp"
}
下面应用程序代码中带有 readLine
的行应该是阻塞的,但它不是:
BufferedReader br = new BufferedReader(new InputStreamReader( System.in ));
String enteredLine = "";
while( enteredLine == null || ! enteredLine.equals( "q" )){
System.out.println( "spuds");
enteredLine = br.readLine();
}
...相反,事情永远旋转:
spuds
spuds
spuds
...
注意我在 Windows 10 OS, Java 8.91。我已经尝试了 Windows DOS 控制台和 Cygwin。
NB2 当我在 Eclipse 中 运行 这个 stream
任务(Gradle STS Eclipse 插件)时发生同样的事情......但当我做 Run as --> Java application
时不会发生:然后阻塞发生为预期。
哈......其中之一是你认为你会永远被难住的地方,你在发布到 SO 后 2 分钟找到了解决方案!我会把它留给其他人...
答案是将行 standardInput =
放在您 运行 的任务中,如下所示:
task stream( type: JavaExec, dependsOn: assemble ){
standardInput = System.in
classpath sourceSets.main.runtimeClasspath
main = "misc.StreamsExp"
}
奇怪的是,在 Windows DOS 终端中提示 "spuds" 之后是
> Building 88% > :stream
... 这是我引用的问题中提到的已知 "bug"。在 Cygwin 中,不会发生此错误。
警告:这在 Windows DOS 终端和 Cygwin 终端中有效...当 运行 定制 NOT 解决问题 stream
Eclipse 中的任务!
This question 解释了在 运行 将任务 运行 项目中的特定 class 时如何使用 System.in
。
但目前对我来说它不起作用:尽管我在 build.gradle
中包含了 application
插件和以下行:
mainClassName = "misc.StreamsExp"
run{
standardInput = System.in
}
task stream( type: JavaExec, dependsOn: assemble ){
classpath sourceSets.main.runtimeClasspath
main = "misc.StreamsExp"
}
下面应用程序代码中带有 readLine
的行应该是阻塞的,但它不是:
BufferedReader br = new BufferedReader(new InputStreamReader( System.in ));
String enteredLine = "";
while( enteredLine == null || ! enteredLine.equals( "q" )){
System.out.println( "spuds");
enteredLine = br.readLine();
}
...相反,事情永远旋转:
spuds
spuds
spuds
...
注意我在 Windows 10 OS, Java 8.91。我已经尝试了 Windows DOS 控制台和 Cygwin。
NB2 当我在 Eclipse 中 运行 这个 stream
任务(Gradle STS Eclipse 插件)时发生同样的事情......但当我做 Run as --> Java application
时不会发生:然后阻塞发生为预期。
哈......其中之一是你认为你会永远被难住的地方,你在发布到 SO 后 2 分钟找到了解决方案!我会把它留给其他人...
答案是将行 standardInput =
放在您 运行 的任务中,如下所示:
task stream( type: JavaExec, dependsOn: assemble ){
standardInput = System.in
classpath sourceSets.main.runtimeClasspath
main = "misc.StreamsExp"
}
奇怪的是,在 Windows DOS 终端中提示 "spuds" 之后是
> Building 88% > :stream
... 这是我引用的问题中提到的已知 "bug"。在 Cygwin 中,不会发生此错误。
警告:这在 Windows DOS 终端和 Cygwin 终端中有效...当 运行 定制 NOT 解决问题 stream
Eclipse 中的任务!