Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle run task accepts input when run in console, but not as an IDEA run configuration

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:

IDEA Run configuration

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.

like image 978
Michele De Pascalis Avatar asked Oct 22 '15 20:10

Michele De Pascalis


People also ask

How to create a run configuration in Gradle?

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.

How do I run a Gradle task from a context menu?

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.

What are inputs and outputs in Gradle?

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.

How do I run a Gradle program in Ubuntu terminal?

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.


1 Answers

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.

like image 127
JBaruch Avatar answered Oct 11 '22 13:10

JBaruch