Unable to start Podman containers using ProcessBuilder

1 week ago 17
ARTICLE AD BOX

You should check both the exit code of the command and what it prints to both stdout (standard output) and stderr (standard error output).

import java.io.*; public class Mcve { public static void main(String... args) throws Exception { ProcessBuilder builder = new ProcessBuilder("ls", "Mcve.java"); Process process = builder.start(); printStreams(process); System.out.println(process.exitValue()); } private static void printStreams(Process process) throws Exception { printStream("stdout", process.getInputStream()); printStream("stderr", process.getErrorStream()); System.out.println(process.waitFor()); } private static void printStream(String what, InputStream stream) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(stream)); String line; while ((line = br.readLine()) != null) { System.out.println(what + ": " + line); } } }

This runs ls and prints the result plus exit code 0 (success).

Same thing for podman? If I change the code to use this:

ProcessBuilder builder = new ProcessBuilder("podman","run 4445:4444 -p 7900:7900 --shm-size=\"2g\" selenium/standalone-firefox:latest"); Process process = builder.start(); printStreams(process); System.out.println(process.waitFor());

it gives me:

output: Error: unrecognized command podman run 4445:4444 -p 7900:7900 --shm-size="2g" selenium/standalone-firefox:latest
output: Try 'podman --help' for more information

This means, ProcessBuilder did find the podman command. The specific error message "Try 'podman --help'" is a good indicator. If you added the builder.directory(new File("/bin")); to make sure it finds podman, you can skip that. On my system podman is in /usr/bin, not in /bin anyway, and as seen with ls, it finds the command. If you pass an invalid executable as the first argument to ProcessBuilder, you'll get a different error:

Exception in thread "main" java.io.IOException: Cannot run program "whatever": error=2, No such file or directory

As David Maze pointed out, you can't cram all arguments into one string, you need to pass them individually:

Change

new ProcessBuilder("podman","run 4445:4444 -p 7900:7900 --shm-size=\"2g\" selenium/standalone-firefox:latest");

to

new ProcessBuilder("podman", "run", "4445:4444", "-p", "7900:7900", "--shm-size=\"2g\"", "selenium/standalone-firefox:latest");

This gives me a different podman error cause I don't have Selenium or whatever your setup needs.

That said, some commands may take longer than others. ls for 1 file is fairly quick, but with ls for a few hundred files, podman ps, or other longer running commands, you may have to wait for the process to finish.

Read Entire Article