This question解释了在运行任务以在项目中运行特定类时如何使用System.in。
但对我来说,它目前不起作用:尽管我已经在build.gradle中包含了build.gradle插件和下面的行
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();
}..。相反,它只是永远地旋转着:
豆芽 豆芽 豆芽 ..。
我在Windows 10操作系统上,使用Java 8.91。我试过Windows DOS控制台和Cygwin。
NB2 --当我在Eclipse中运行这个stream任务(Gradle STS Eclipse插件)时,也会发生同样的事情。但是当我执行Run as --> Java application时就不会了:那么阻塞就会像预期的那样发生。
发布于 2016-10-25 18:25:16
哈..。其中一个坐在你认为你将永远困惑和你找到解决方案2分钟后,张贴到这样!我会把它留给其他任何人。
答案是将行standardInput =放在正在运行的任务中,如下所示:
task stream( type: JavaExec, dependsOn: assemble ){
standardInput = System.in
classpath sourceSets.main.runtimeClasspath
main = "misc.StreamsExp"
}奇怪的是,在Windows DOS终端中,提示"spuds“后面跟着
建筑物88% >:溪流
..。这是我提到的问题中提到的一个已知的"bug“。在Cygwin,这个bug不会发生。
警告:这适用于Windows DOS终端和Cygwin终端.它做了,而不是,解决了在Eclipse中运行定制stream任务时的问题!
https://stackoverflow.com/questions/40246732
复制相似问题