使用gradle run启动应用程序时,java.util.scanner引发NoSuchElementException


问题内容

我创建了一个简单的java“
echo”应用程序,该应用程序接收用户的输入并将其显示给他们以演示该问题。我可以使用IntelliJ的内部“运行”命令运行该应用程序,并且在执行产生的已编译Java文件时也可以正常运行gradle build。但是,如果尝试使用执行应用程序,则会gradle run从扫描仪抛出NoSuchElementException异常。

我认为gradle或应用程序插件正在对系统IO做一些奇怪的事情。

应用

package org.gradle.example.simple;

import java.util.Scanner;

public class HelloWorld {
  public static void main(String args[]) {
    Scanner input = new Scanner(System.in);
    String response = input.nextLine();
    System.out.println(response);
  }
}

build.gradle

apply plugin: 'java'
version '1.0-SNAPSHOT'

apply plugin: 'java'

jar {
    manifest {
        attributes 'Main-Class': 'org.gradle.example.simple.HelloWorld'
    }
}

apply plugin: 'application'

mainClassName = "org.gradle.example.simple.HelloWorld"

sourceCompatibility = 1.5

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

任何想法如何使该应用程序使用gradle run


问题答案:

您必须将默认的stdin连接到gradle,并将其放入build.gradle中:

run{
    standardInput = System.in
}