Core Library0.xHome

Choose documentation 3

latest

Current version

latest
English

Early release libtmux is released, and its APIs may still change between releases. This site's structure and URLs may change too.

Edit this page on GitHub

tmux client library for Java

Control tmux, connect to and create servers, navigate sessions, windows, and panes, send keys, watch for changes via control mode, capture pane contents.

Install

implementation("io.github.libtmux:libtmux:0.1.0")

At a glance

Server → Session → Window → Pane. Each level of the tmux hierarchy has its own entry in the Java API, with the tmux object it wraps.

TypeWraps
io.github.libtmux.Server A tmux server
io.github.libtmux.Session A session, ID $n
io.github.libtmux.Window A window in a session, ID @n
io.github.libtmux.Pane A pane in a window, ID %n

What it does

Start with Java

Example details
import io.github.libtmux.Server;
import io.github.libtmux.ServerConfig;
import io.github.libtmux.ServerEndpoint;
import io.github.libtmux.TextOutcome;
import java.nio.file.Path;
import java.time.Duration;
import java.util.UUID;
public final class Main {
public static void main(String[] args) throws Exception {
var socket = "libtmux-home-" + UUID.randomUUID();
var config = ServerConfig.builder()
.endpoint(ServerEndpoint.namedSocket(socket))
.configFile(Path.of("/dev/null"))
.defaultTimeout(Duration.ofSeconds(5))
.build();
try (Server server = Server.open(config)) {
var session = server.newSession("demo");
var window = session.newWindow("build");
var pane = window.split();
pane.sendLine("printf '\\nhello from %s\\n' libtmux");
var seen = pane.awaitText(
"hello from libtmux",
Duration.ofSeconds(5));
if (seen == TextOutcome.TIMED_OUT) {
throw new IllegalStateException("output did not arrive");
}
var lines = pane.capture();
if (lines.stream().noneMatch(
line -> line.strip().equals("hello from libtmux"))) {
throw new IllegalStateException(
"capture is missing the output line");
}
System.out.println("hello from libtmux");
}
}
}
import io.github.libtmux.Server;
import io.github.libtmux.ServerConfig;
import io.github.libtmux.ServerEndpoint;
import io.github.libtmux.TextOutcome;
import java.nio.file.Path;
import java.time.Duration;
import java.util.UUID;
public final class Main {
public static void main(String[] args) throws Exception {
var socket = "libtmux-home-" + UUID.randomUUID();
var config = ServerConfig.builder()
.endpoint(ServerEndpoint.namedSocket(socket))
.configFile(Path.of("/dev/null"))
.defaultTimeout(Duration.ofSeconds(5))
.build();
try (Server server = Server.open(config)) {
try {
var session = server.newSession("demo");
var window = session.newWindow("build");
var pane = window.split();
pane.sendLine("printf '\\nhello from %s\\n' libtmux");
var seen = pane.awaitText(
"hello from libtmux",
Duration.ofSeconds(5));
if (seen == TextOutcome.TIMED_OUT) {
throw new IllegalStateException("output did not arrive");
}
var lines = pane.capture();
if (lines.stream().noneMatch(
line -> line.strip().equals("hello from libtmux"))) {
throw new IllegalStateException(
"capture is missing the output line");
}
System.out.println("hello from libtmux");
} catch (Exception error) {
throw new IllegalStateException(
"tmux quickstart failed", error);
}
}
}
}
import io.github.libtmux.Server;
import io.github.libtmux.ServerConfig;
import io.github.libtmux.ServerEndpoint;
import io.github.libtmux.TextOutcome;
import java.nio.file.Path;
import java.time.Duration;
import java.util.UUID;
public final class Main {
public static void main(String[] args) throws Exception {
var socket = "libtmux-home-" + UUID.randomUUID();
var config = ServerConfig.builder()
.endpoint(ServerEndpoint.namedSocket(socket))
.configFile(Path.of("/dev/null"))
.defaultTimeout(Duration.ofSeconds(5))
.build();
try (Server server = Server.open(config)) {
try {
var session = server.newSession("demo");
var window = session.newWindow("build");
var pane = window.split();
pane.sendLine("printf '\\nhello from %s\\n' libtmux");
var seen = pane.awaitText(
"hello from libtmux",
Duration.ofSeconds(5));
if (seen == TextOutcome.TIMED_OUT) {
throw new IllegalStateException("output did not arrive");
}
var lines = pane.capture();
if (lines.stream().noneMatch(
line -> line.strip().equals("hello from libtmux"))) {
throw new IllegalStateException(
"capture is missing the output line");
}
System.out.println("hello from libtmux");
} finally {
server.killServer();
}
}
}
}
import io.github.libtmux.Server;
import io.github.libtmux.ServerConfig;
import io.github.libtmux.ServerEndpoint;
import io.github.libtmux.TextOutcome;
import java.nio.file.Path;
import java.time.Duration;
import java.util.UUID;
public final class Main {
public static void main(String[] args) throws Exception {
var socket = "libtmux-home-" + UUID.randomUUID();
var config = ServerConfig.builder()
.endpoint(ServerEndpoint.namedSocket(socket))
.configFile(Path.of("/dev/null"))
.defaultTimeout(Duration.ofSeconds(5))
.build();
try (Server server = Server.open(config)) {
try {
var session = server.newSession("demo");
var window = session.newWindow("build");
var pane = window.split();
pane.sendLine("printf '\\nhello from %s\\n' libtmux");
var seen = pane.awaitText(
"hello from libtmux",
Duration.ofSeconds(5));
if (seen == TextOutcome.TIMED_OUT) {
throw new IllegalStateException("output did not arrive");
}
var lines = pane.capture();
if (lines.stream().noneMatch(
line -> line.strip().equals("hello from libtmux"))) {
throw new IllegalStateException(
"capture is missing the output line");
}
System.out.println("hello from libtmux");
} catch (Exception error) {
throw new IllegalStateException(
"tmux quickstart failed", error);
} finally {
server.killServer();
}
}
}
}
Run this example

JDK 25 and tmux on Linux/macOS

Save the program above as src/main/java/Main.java in a new directory, along with the files below.

Cleanup is omitted; the private tmux server and its temporary files may remain. Select Cleanup to remove them when the program finishes. The program stops its private tmux server and removes any temporary files it created when it finishes. Error handling adds diagnostics; every version checks required results and deadlines.

build.gradle.kts
plugins {
application
}
repositories { mavenCentral() }
dependencies {
implementation("io.github.libtmux:libtmux:0.1.0")
}
java { toolchain { languageVersion.set(JavaLanguageVersion.of(25)) } }
application { mainClass.set(providers.gradleProperty("exampleMain")) }
settings.gradle.kts
rootProject.name = "api-example"
gradle.properties
org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=768m -Dfile.encoding=UTF-8
kotlin.daemon.jvmargs=-Xmx2g
org.gradle.workers.max=2

Setup and run

# Save the files above at their displayed relative paths.
git clone https://github.com/libtmux/libtmux-java.git libtmux-source
git -C libtmux-source checkout --detach 5c181887918351119bfdb3a046719fa792048e59
./libtmux-source/gradlew --project-dir . --console=plain --no-daemon --max-workers=2 \
installDist -PexampleMain=Main
SHELL=/bin/sh build/install/api-example/bin/api-example

Expected output

hello from libtmux

Library revision: 5c1818879183.

Typed API

What you get over the tmux CLI

Testing

Run tests against an isolated tmux server: io.github.libtmux.junit5.TmuxExtension.

Esc

Type to search.

Reference previewOpen reference