Pty for Java


Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositories {
			maven { url '' }

Add it in your build.sbt at the end of resolvers:

    resolvers += "jitpack" at ""

Add it in your project.clj at the end of repositories:

    :repositories [["jitpack" ""]]

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.asciidocfx:pty4j:'

    libraryDependencies += "com.github.asciidocfx" % "pty4j" % ""

    :dependencies [[com.github.asciidocfx/pty4j ""]]


Pty4J - Pseudo terminal(PTY) implementation in Java.

This is a pure Java implementation of PTY. Written in JNA with native code to make fork of a process.

It is based on two projects that provide the same functionality: JPty ( and elt (

While JPty is pretty good and written using only JNA it suffers from a hang on Mac OS X when Java is under debug (, because fork doesn't work well in java.

elt works ok, but it has mostly native code(using JNI).

So this one is a mix of the other two: a port of elt to JNA in the style it is made in JPty with only fork and process exec written in native.

Also pty4j implements java interface for pty for windows, using win-pty library.


This library depends on JTermios, part of the PureJavacomm library found at A binary release of this library, along with its dependency JNA, is made part of this repository, and can be found in the lib-directory.

Windows pty implementation used here is the fork of WinPty by Ryan Prichard ( located here


Using this library is relatively easy:

// The command to run in a PTY...
String[] cmd = { "/bin/sh", "-l" };
// The initial environment to pass to the PTY child process...
String[] env = { "TERM=xterm" };

PtyProcess pty = PtyProcess.exec(cmd, env);

OutputStream os = pty.getOutputStream();
InputStream is = pty.getInputStream();

// ... work with the streams ...

// wait until the PTY child process terminates...
int result = pty.waitFor();

// free up resources.

The operating systems currently supported by pty4j are: Linux, OSX and Windows.

Note that this library is not yet fully tested on all platforms.


0.7 | 14-06-2016 | Updated to match the new winpty API
0.6 | 08-02-2016 | Correct NamedPipe usage for winpty
                   FreeBSD support
0.5 | 28-09-2015 | Supports JNA 4.1, Cygwin support, fixes
0.4 | 13-01-2015 | Fixes
0.3 | 16-08-2013 | Native code for fork and process exec.
0.2 | 03-08-2013 | Linux and Windows supported.
0.1 | 20-07-2013 | Initial version.


The code in this library is licensed under Eclipse Public License, version 1.0 and can be found online at: