4

I'm trying out the new JShell program with the java9 jdk. I want to debug an interactive console app I'm making, but it seems System.in doesn't work. For example:

[root@mycomputer home]# jdk-9/bin/jshell 
|  Welcome to JShell -- Version 9-ea
|  Type /help for help

-> new Scanner(System.in).next()
 

It just freezes here and locks the keyboard up completely. I have to kill the process in another terminal in order to get my prompt back.

As mlk points out, it looks like this is a known bug. Has anyone found a workaround?


My specs: Redhat x86_64, Gnome-Terminal, Java9 Build 109 
Maroun
  • 94,125
  • 30
  • 188
  • 241
flakes
  • 21,558
  • 8
  • 41
  • 88

2 Answers2

1

The only workaround I found is assigning the "input" to a string, and use Scanner's constructor that accepts a String:

public Scanner(String source)

Constructs a new Scanner that produces values scanned from the specified string.


-> String input = "Hello world"
|  Added variable input of type String with initial value "Hello world"

-> Scanner sc = new Scanner(input)
|  Modified variable sc of type Scanner with initial value java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\QNaN\E][infinity string=\Q∞\E]

-> sc.next()
|  Expression value is: "Hello"
|    assigned to temporary variable $5 of type String
Maroun
  • 94,125
  • 30
  • 188
  • 241
  • this solution doesn't really work for me. I'd have to rip out a lot of code. I've been playing around with redirecting system.in to a custom input stream and creating the scanner on a different thread – flakes Mar 15 '16 at 15:24
  • @flkes JShell is not meant to be used for writing complicated code parts. It should be used to test little things, not implementing multithreaded projects. For that, I would still create a test project using an IDE. – Maroun Mar 15 '16 at 15:27
  • 2
    I'm not writing the code in jshell, just debugging it. I should be able to use jshell and test my api. – flakes Mar 15 '16 at 15:29
  • 1
    @flkes I hope it'll be fixed in the next release. – Maroun Mar 16 '16 at 08:43
1

This was a bug and it has been fixed (prior to the release of JDK 9).

jshell> new Scanner(System.in).next()
r
$1 ==> "r"
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
Robert Field
  • 479
  • 4
  • 5