Java workspace builder examples
libtmux-workspace 0.1.2-SNAPSHOT · Source
Build a two-window workspace, inspect its panes, and remove the private tmux
server. Use a POSIX host with tmux on PATH and JDK 25.
Create the project¶
In a new directory, fetch the 0.1.0 release, which supplies the Gradle wrapper:
$ mkdir java-workspace-example$ cd java-workspace-example$ git init -q libtmux-source$ git -C libtmux-source remote add origin https://github.com/libtmux/libtmux-java.git$ git -C libtmux-source fetch --depth=1 origin 5c181887918351119bfdb3a046719fa792048e59$ git -C libtmux-source checkout --detach FETCH_HEAD$ mkdir -p src/main/javaSave the following settings file:
rootProject.name = "workspace-example"Save the following build file. Gradle resolves the workspace module and its core dependency from Maven Central:
plugins { application}
repositories { mavenCentral()}
dependencies { implementation("io.github.libtmux:libtmux-workspace:0.1.0")}
java { toolchain.languageVersion.set(JavaLanguageVersion.of(25))}
application { mainClass.set("WorkspaceExample")}Build and inspect¶
Save this complete program as src/main/java/WorkspaceExample.java:
import io.github.libtmux.Server;import io.github.libtmux.ServerEndpoint;import io.github.libtmux.Session;import io.github.libtmux.workspace.Workspace;import io.github.libtmux.workspace.WorkspaceBuilder;import java.nio.file.Path;import java.time.Duration;import java.util.UUID;
public final class WorkspaceExample { public static void main(String[] args) { Workspace workspace = WorkspaceBuilder.parse(""" session_name: built windows: - window_name: editor layout: even-horizontal panes: - shell_command: echo one - shell_command: echo two - window_name: server panes: - echo three """); try (Server server = Server.builder() .endpoint(ServerEndpoint.namedSocket("workspace-" + UUID.randomUUID())) .configFile(Path.of("/dev/null")) .defaultTimeout(Duration.ofSeconds(10)) .build()) { try { server.newSession("bootstrap"); Session session = WorkspaceBuilder.build(server, workspace); System.out.println(session.name() + ": " + session.windows().size() + " windows"); System.out.println("editor: " + session.windows().getFirst().panes().size() + " panes"); } finally { server.killServer(); } } }}Use the checked-out Gradle wrapper to run the application:
$ ./libtmux-source/gradlew --no-daemon --max-workers=2 -p . runThe application prints built: 2 windows and editor: 2 panes. Its unique
socket prevents a collision with an existing server. The finally block
removes that server even if construction or inspection fails. Closing the
Java handle then releases its transport.
The bootstrap session starts tmux before the workspace builder checks its version. It belongs to the same private server and is removed with it.
Building creates the layout and sends the pane commands. It does not wait for
those programs to finish. Keep the session running instead of calling
killServer when adapting this example into an application launcher.