0

When using JShell's /open command to open a Java file and run code, user input does not work with Scanner. It will always throw a NoSuchElementException when it reaches the nextLine() method. Does anyone know why this is, and is there a fix/workaround that I am misssing?

Steps to reproduce:

  • Create a Java file with the following code (for example, test.java):
Scanner scanner = new Scanner(System.in)
String input = scanner.nextLine();
  • Open JShell
  • Run /open test.java

I have already looked at this answer but using the --execution local did not solve my issue. Calling jshell --execution local and then /open test.java gets a little closer where the program does halt for user input, but then the shell seems to freeze and I cannot type anything. Calling jshell --execution local test.java does seem to work, but after running the file it keeps you in JShell (where the /open command will not work), and it's really inconvenient to have to keep exiting out of JShell to rerun a file. The alternative is having each script end with System.exit(0);, but I don't really understand why something as fundamental as user input appears to be so bugged.

I am using OpenJDK version 17.0.5.

Normally this wouldn't be that big of a deal, but I was hoping to use JShell as a teaching tool...but these input issues/inconveniences are concerning me. Any help is greatly appreciated!

0 Answers0