tl;dr
Use new java.io.IO class in Java 25 and later.
String input = IO.readln ( "Enter text: " );
System.console does work
Calling System.console() from the IntelliJ IDE does work for me.
On IntelliJ IDEA 2024.3 Beta, with Java 23.0.1, on a MacBook Pro with M1 Pro Apple Silicon running macOS Sonoma 14.7.1, I run this code:
package work.basil.example.console; public class TryIO { public static void main ( String[] args ) { System.console ( ).printf ( "Hello world!" + System.lineSeparator ( ) ); System.console ( ).printf ( "Enter text: " ); String input = System.console ( ).readLine ( ); System.console ( ).printf ( "You typed: " + input + System.lineSeparator ( ) ); } }
… and get:
Hello world!
Enter text: Bonjour
You typed: Bonjour
print, println, & readln methods on Console
In Java 25 is a new feature that adds methods to the Console class.
So we can rewrite the code above while avoiding printf, omitting the explicit line-terminator, and passing a user-prompt.
System.console ( ).println ( "Hello world!" ); String input = System.console ( ).readln ( "Enter text: " ); System.console ( ).println ( "You typed: " + input );
When run:
Hello world!
Enter text: Bonjour
You typed: Bonjour
For more info, see another Question: Newer easier ways to interact with the console in Java 23+?.
java.io.IO
As part of that new feature in Java 25+, we get a java.io.IO class with static methods for convenient access to those new Console methods.
This IO class implicitly accesses the Console object returned by System.console(). This IO class provides the same method signatures as are being added to Console:
So we can more briefly write the code above.
IO.println ( "Hello world!" ); String input = IO.readln ( "Enter text: " ); IO.println ( "You typed: " + input );
Same output as seen above.
You could of course use static import to drop the IO. prefix. But that is not my style.
IOpreviewed in Java 24, shown in my Answer below.