I have the following Java main class which 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 a terminal everything works as expected. However there is this IDEA run configuration that I would like to use to test-run:
Everything is fine until I do some console input. in.nextLine()
throws the following exception:
Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1585) at com.mikidep.bookshop.Main.main(Main.java:10)
I also tried to debug it and noticed that all fields in System.in
seem zeroed out, like the whole stream hadn't been initialized. Anyone knows what's happening?
EDIT: I just verified that this affects the build script as well: I added this lines to the run task
System.out.println("Wait...")
System.in.read()
System.out.println("Ok!")
But Gradle prints those two lines right away, without waiting for my input.
1 Open the Gradle tool window. 2 Right-click the task for which you want to create the Run configuration. 3 From the context menu select Create 'task name'. 4 In Create Run/Debug Configuration: 'task name', specify the task settings and click OK. ... 5 Double-click the task to run it or right-click the task and from the context menu select Run.
Run Gradle tasks. You can use several ways to run Gradle tasks such as run them from the Run Anything window, with a run configuration, from a context menu, and even run several tasks with one run configuration. In the Gradle tool window, on the toolbar, click . Alternatively, press Ctrl twice to open the Run Anything window.
The Property class also knows about which task it’s linked to, so using inputs and outputs in this way enables Gradle to automatically add the required task dependency. To understand how this works, here are some input types and their equivalent property-based type.
In the Gradle tool window, on the toolbar, click . Alternatively, press Ctrl twice to open the Run Anything window. In the Run Anything window, start typing a name of the task you want to execute. To execute several tasks, enter task names using space to separate each new task.
System.in
is not the Script parameters.
In command line, since you don't specify any parameters that Gradle expects, it just uses the remaining of what you typed as standard input. In IDEA the standard input and the script parameters are separated and the standard input comes from the run or debug console window.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With