Gradle Launching a task accepts input at startup in the console, but not as an IDEA startup configuration

I have the following main Java class that I am trying to compile and run using the Gradle plugin in IntelliJ IDEA:

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!"); } } 

My build.gradle follows:

 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 } 

Now, if I run gradle run -q in the terminal, everything works as expected. However, there is such an IDEA startup configuration that I would like to use for testing:

IDEA Launch Configuration

Everything is fine until I make console input. in.nextLine() throws the following exception:

Exception in thread "main" java.util.NoSuchElementException: No line found on java.util.Scanner.nextLine (Scanner.java:1585) in com.mikidep.bookshop.Main.main (Main.java:10)

I also tried debugging it and noticed that all the fields in System.in seem to be nullified, for example, the whole thread was not initialized. Does anyone know what is going on?

EDIT: I just confirmed that this also affects the build script: I added these lines to the execution task

 System.out.println("Wait...") System.in.read() System.out.println("Ok!") 

But Gradle prints these two lines at once, without waiting for input.

+6
source share
1 answer

System.in not a Script parameter.

On the command line, since you are not specifying any parameters expected by Gradle, it simply uses the remaining data that you entered as standard input. In IDEA, the standard input and Script options are separated, and the standard input comes from the console launch or debug window.

+1
source

All Articles