Terminal Providers
JLine supports multiple terminal providers, each with its own characteristics and capabilities. This allows JLine to work across different platforms and environments.
JLine 4.x has removed the JNA and Jansi providers. These providers are no longer available and have been replaced by more modern alternatives.
The recommended providers for JLine 4.x are JNI (for maximum compatibility) and FFM (for best performance on Java 22+).
Provider Selection
JLine can automatically select the best terminal provider for your environment, or you can explicitly specify which provider to use:
Loading snippet: ProviderSelectionExample...
JNA Provider (Removed in JLine 4.x)
The JNA (Java Native Access) provider has been removed in JLine 4.x. Use the JNI or FFM providers instead for native terminal functionality.
Jansi Provider (Removed in JLine 4.x)
The Jansi provider has been removed in JLine 4.x. Use the JNI or FFM providers instead for ANSI support and terminal functionality.
FFM Provider
The FFM (Foreign Function & Memory) provider uses Java's FFM API (available in Java 22+) to access native terminal functionality:
Loading snippet: FfmTerminalExample...
JNI Provider
The JNI (Java Native Interface) provider uses JNI to access native terminal functionality:
Loading snippet: JniTerminalExample...
Exec Provider
The Exec provider uses external commands to access terminal functionality:
Loading snippet: ExecTerminalExample...
Dumb Terminal
The Dumb terminal is a fallback option that provides basic terminal functionality without advanced features:
Loading snippet: DumbTerminalExample...
Custom ClassLoader Support
JLine 4.x introduces support for custom classloaders when loading terminal providers. This is essential for applications that use custom classloader architectures, such as:
- Plugin systems and modular applications
- OSGi containers
- Application servers with custom classloading
- Environments where JLine is loaded in a different classloader
Using Custom ClassLoaders
You can specify a custom classloader when building a terminal:
Loading snippet: CustomClassLoaderTerminal...
Common Use Cases
Plugin Systems: When your application loads JLine as part of a plugin or module:
Loading snippet: PluginClassLoaderTerminal...
OSGi Environments: When working in OSGi containers:
Loading snippet: OSGiClassLoaderTerminal...
Troubleshooting ClassLoader Issues
If you encounter "Unable to find terminal provider" errors, it's often due to classloader issues. Try:
- Check your classloader: Ensure JLine and its providers are accessible from your classloader
- Use custom classloader: Explicitly set the classloader that can access JLine providers
- Verify provider availability: Check that the required terminal providers are in your classpath
- Enable debug logging: Use JLine's logging to see which providers are being attempted
Advanced ClassLoader Scenarios
Custom Module Systems: When working with custom module systems or frameworks:
Loading snippet: ModuleSystemClassLoaderTerminal...
Testing with Mock ClassLoaders: For unit testing scenarios:
Loading snippet: TestClassLoaderTerminal...
Fallback Strategy: Implement a robust fallback strategy:
Loading snippet: FallbackClassLoaderStrategy...
Best Practices
When working with terminal providers in JLine, consider these best practices:
-
Auto-Selection: Let JLine automatically select the best provider for your environment when possible.
-
Fallback Strategy: Implement a fallback strategy if a specific provider is not available.
-
Feature Detection: Check for terminal capabilities before using advanced features.
-
Cross-Platform Testing: Test your application on different platforms to ensure it works with different providers.
-
Error Handling: Handle terminal-related errors gracefully.
-
Terminal Cleanup: Always close the terminal when your application exits.
-
Signal Handling: Handle terminal signals (like SIGINT) appropriately.
-
Terminal Size: Be aware of terminal size and adapt your UI accordingly.
-
Color Support: Check for color support before using colors.
-
Documentation: Document which terminal providers your application supports.