You need to run your program in some way that doesn't line-buffer user input.
Lots of detail here and some related discussion here.
This code:
public static void main(String[] args) throws IOException {
System.out.print("hit a key: ");
System.out.print(System.in.read());
}
and a "Terminal" app on OS X with this:
stty raw -echo
behaves like this when run (where the above code is in a file named Scratch.java
, and I typed a single A
as input):
$ stty raw && java Scratch
hit a key: 65