Gradle运行任务在控制台中运行时接受输入,但不作为IDEA运行配置

Mic*_*lis 6 java intellij-idea gradle

我有以下Java主类,我试图使用IntelliJ IDEA中的Gradle插件编译和运行:

package com.mikidep.bookshop;

import java.io.IOException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        System.out.print("Inserisci testo qui: ");
        System.out.println(in.nextLine());
        System.out.println("Yee!");
    }
}
Run Code Online (Sandbox Code Playgroud)

我的build.gradle如下:

group 'com.mikidep.bookshop'
version '1.0-SNAPSHOT'

apply plugin: 'application'

mainClassName = "com.mikidep.bookshop.Main"

sourceCompatibility = 1.5

repositories {
    mavenCentral()
}

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

run {
    standardInput = System.in
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我gradle run -q在终端中运行,一切都按预期工作.但是,我想使用这个IDEA运行配置进行测试运行:

IDEA运行配置

一切都很好,直到我做一些控制台输入.in.nextLine()抛出以下异常:

线程"main"java.util.NoSuchElementException中的异常:在com.mikidep.bookshop.Main.main(Main.java:10)的java.util.Scanner.nextLine(Scanner.java:1585)中找不到任何行

我也尝试调试它,并注意到所有字段System.in都显示为零,就像整个流尚未初始化一样.谁知道发生了什么?

编辑:我刚刚验证了这也影响了构建脚本:我将这些行添加到了运行任务中

System.out.println("Wait...")
System.in.read()
System.out.println("Ok!")
Run Code Online (Sandbox Code Playgroud)

但Gradle立即打印这两行,而不是等待我的输入.

JBa*_*uch 2

System.in不是脚本参数。

在命令行中,由于您没有指定 Gradle 期望的任何参数,因此它仅使用您键入的其余内容作为标准输入。在IDEA中,标准输入和脚本参数是分开的,标准输入来自运行或调试控制台窗口。