Capture pane output
Needs libtmux for Java 0.x Install
A pane runs asynchronously: sending a command does not mean its output is already on screen. Capture repeatedly until the expected line appears, with a deadline so a failed command cannot leave the program waiting forever.
This complete program creates a private tmux server, captures its output, and cleans up. Follow the setup and run instructions below. You need tmux 3.3a or newer and a Unix environment; no existing tmux session is required.
Read what’s on screen
The program sends printf with a leading newline, then waits for the complete
line libtmux capture ready. The newline keeps a late shell prompt off that
line. Matching the whole line avoids mistaking the echoed command for its output.
import io.github.libtmux.Pane;import io.github.libtmux.Server;import io.github.libtmux.ServerConfig;import io.github.libtmux.ServerEndpoint;import io.github.libtmux.Session;import java.nio.file.Files;import java.nio.file.Path;import java.time.Duration;import java.util.List;
public final class Capture { public static void main(String[] args) throws Exception { Path directory = Files.createTempDirectory("libtmux-capture-"); Path socket = directory.resolve("tmux.sock"); ServerConfig config = ServerConfig.builder() .endpoint(ServerEndpoint.socketPath(socket)) .configFile(Path.of("/dev/null")) .defaultTimeout(Duration.ofSeconds(1)) .build(); try (Server server = Server.open(config)) { try { Session session = server.newSession(s -> s .named("capture").running("sh").env("ENV", "/dev/null")); Pane pane = session.windows().get(0).panes().get(0); pane.sendLine("printf '\\nlibtmux capture ready\\n'"); long deadline = System.nanoTime() + Duration.ofSeconds(5).toNanos(); while (System.nanoTime() < deadline) { List<String> lines = pane.capture(); if (lines.contains("libtmux capture ready")) { System.out.println("libtmux capture ready"); return; } Thread.sleep(20); } throw new IllegalStateException("Timed out waiting for pane output"); } finally { server.killServer(); } } finally { Files.deleteIfExists(socket); Files.delete(directory); } }}Wait for output or completion
The program above checks the captured screen for up to five seconds. The short pause between checks limits polling; the observed output determines when the loop finishes. A tmux capture is a view of the screen and scrollback, so it can miss output that has already scrolled away. Use a stream or a completion signal for long-running commands when that distinction matters.
Capturing output covers capture options, while Sending keys explains why sending and waiting are separate operations.
Setup and run
Use an empty directory and save the files using the displayed names. You need
JDK 25 and Gradle 9. The build file installs io.github.libtmux:libtmux 0.1.2
from Maven Central.
Save settings.gradle.kts beside Capture.java.
rootProject.name = "capture"Save build.gradle.kts beside Capture.java.
plugins { application}
repositories { mavenCentral() }dependencies { implementation("io.github.libtmux:libtmux:0.1.2")}java { toolchain { languageVersion.set(JavaLanguageVersion.of(25)) } }sourceSets.main { java.srcDir("."); java.include("Capture.java") }application { mainClass.set("Capture") }Build and run the program:
$ gradle --console=plain --no-daemon runWhere this comes from
This program uses io.github.libtmux:libtmux 0.1.2 from Maven Central. Its
source
is on GitHub.