Class TerminalBuilder

java.lang.Object
org.jline.terminal.TerminalBuilder

public final class TerminalBuilder extends Object
Builder class to create Terminal instances with flexible configuration options.

TerminalBuilder provides a fluent API for creating and configuring terminals with various characteristics. It supports multiple implementation providers and handles the complexities of terminal creation across different platforms and environments.

Terminal Providers

JLine supports multiple terminal provider implementations:

  • FFM - Foreign Function Memory (Java 22+) based implementation
  • JNI - Java Native Interface based implementation
  • Jansi - Implementation based on the Jansi library
  • JNA - Java Native Access based implementation
  • Exec - Implementation using external commands
  • Dumb - Fallback implementation with limited capabilities

The provider selection can be controlled using the provider(String) method or the org.jline.terminal.provider system property. By default, providers are tried in the order: FFM, JNI, Jansi, JNA, Exec.

Native Library Support

When using providers that require native libraries (such as JNI, JNA, or Jansi), the appropriate native library will be loaded automatically. The loading of these libraries is handled by JLineNativeLoader for the JNI provider.

The native library loading can be configured using system properties as documented in JLineNativeLoader.

System vs. Non-System Terminals

TerminalBuilder can create two types of terminals:

  • System terminals - Connected to the actual system input/output streams
  • Non-system terminals - Connected to custom input/output streams

System terminals are created using system(boolean) with a value of true, while non-system terminals require specifying input and output streams using streams(InputStream, OutputStream).

Usage Examples

Creating a default system terminal:

 Terminal terminal = TerminalBuilder.builder()
     .system(true)
     .build();
 

Creating a terminal with custom streams:

 Terminal terminal = TerminalBuilder.builder()
     .name("CustomTerminal")
     .streams(inputStream, outputStream)
     .encoding(StandardCharsets.UTF_8)
     .build();
 

Creating a terminal with a specific provider:

 Terminal terminal = TerminalBuilder.builder()
     .system(true)
     .provider("jni")
     .build();
 

Creating a dumb terminal (with limited capabilities):

 Terminal terminal = TerminalBuilder.builder()
     .dumb(true)
     .build();
 
See Also: