Reading Console Input in Java | Generated by AI

Home PDF

To use the org.jline.reader package in Java, which is part of the JLine library for handling console input with advanced features like line editing, history, and completion, follow these steps. This guide provides a complete and practical approach to get you started.

Prerequisites

Ensure you have the JLine library added to your project. If you’re using Maven, include the following dependency in your pom.xml:

<dependency>
    <groupId>org.jline</groupId>
    <artifactId>jline</artifactId>
    <version>3.21.0</version> <!-- Use the latest version -->
</dependency>

Basic Steps to Use org.jline.reader

  1. Create a Terminal Instance
    • Use the TerminalBuilder class from org.jline.terminal to create a Terminal object. This represents the console environment where input will be read.
    • Example:
      import org.jline.terminal.Terminal;
      import org.jline.terminal.TerminalBuilder;
      
      Terminal terminal = TerminalBuilder.builder().build();
      
    • The build() method creates a default terminal suitable for most environments. You can customize it further (e.g., setting terminal type), but the default is often sufficient.
  2. Create a LineReader Instance
    • Use the LineReaderBuilder class from org.jline.reader to create a LineReader object, passing the Terminal instance to it.
    • The LineReader is the main interface for reading user input with JLine’s features.
    • Example:
      import org.jline.reader.LineReader;
      import org.jline.reader.LineReaderBuilder;
      
      LineReader reader = LineReaderBuilder.builder()
          .terminal(terminal)
          .build();
      
  3. Read Input from the User
    • Use the readLine() method of LineReader to read a line of text entered by the user. You can optionally specify a prompt to display.
    • Example:
      String line = reader.readLine("> ");
      
    • This displays > as a prompt, waits for user input, and returns the entered string when the user presses Enter.

Simple Example

Here’s a complete, minimal example that reads user input in a loop until the user types “exit”:

import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;

public class ConsoleExample {
    public static void main(String[] args) throws Exception {
        // Create Terminal
        Terminal terminal = TerminalBuilder.builder().build();
        
        // Create LineReader
        LineReader reader = LineReaderBuilder.builder()
            .terminal(terminal)
            .build();
        
        // Read input in a loop
        String line;
        while ((line = reader.readLine("> ")) != null) {
            System.out.println("You entered: " + line);
            if (line.equals("exit")) {
                break;
            }
        }
    }
}

Optional Features

You can enhance the LineReader with additional functionality:

1. Enable Command History

2. Add Auto-Completion

3. Read Passwords (Masked Input)

Resource Management

For robust applications, close the Terminal when done to free resources. Since Terminal implements Closeable, use a try-with-resources block:

try (Terminal terminal = TerminalBuilder.builder().build()) {
    LineReader reader = LineReaderBuilder.builder()
        .terminal(terminal)
        .build();
    String line = reader.readLine("> ");
    System.out.println("You entered: " + line);
}

Why Use org.jline.reader?

Unlike basic input with System.in and BufferedReader, org.jline.reader provides:

Notes

This covers the essentials of using org.jline.reader to read console input in Java effectively. Start with the basic setup and add features like history or completion as needed for your application!


Back 2025.03.06 Donate