简体   繁体   中英

Gradle won't allow input via Windows command prompt

On a shell server that has the JDK installed, the following program allows input from the user by reading in the next integer and displaying the correct output:

package hello;
import java.util.Scanner;

public class HelloWorld {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int theNextInt = in.nextInt();
    System.out.println("Next integer is: " + theNextInt);
  }
}

However, when I perform gradlew run on my Windows console, the following error occurs:

Exception in thread "main" java.util.NoSuchElementException
        at java.util.Scanner.throwFor(Scanner.java:862)
        at java.util.Scanner.next(Scanner.java:1485)
        at java.util.Scanner.nextInt(Scanner.java:2117)
        at java.util.Scanner.nextInt(Scanner.java:2076)
        at hello.HelloWorld.main(HelloWorld.java:12)
:run FAILED

Of course, the exception occurs because Scanner is not reading any more elements. That's because the Windows console won't allow me to input. Any way around this?

I figured it out. It turns out that after doing further research, I ran into this question and this question which offer the same solution (that works):

Include the following in the build.gradle file:

run {

standardInput = System.in

}

because according to the docs , the InputStream defaults to empty for a task of type JavaExec .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM