avivblo
avivblo

Reputation: 144

Error when creating zstd file with ProcessBuilder on Java

I'm trying to use ProcessBuilder in order to create a tar.zst file that compresses a folder. However, I'm getting the following error after running this code:

try {
    ProcessBuilder pb = new ProcessBuilder("tar","--zstd","-cf","info-x.tar.zst","tstpkg");
    pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
    pb.redirectError(ProcessBuilder.Redirect.INHERIT);
    Process process = pb.start();
    process.waitFor();
} catch (Exception e){
    e.printStackTrace();
}

Error:

tar: Can't launch external program: zstd --no-check -3

And the tar file is created, but is empty.

If I run the same command from the terminal

tar --zstd -cf info-x.tar.zst tstpkg

then it works fine.

Upvotes: -1

Views: 161

Answers (1)

DuncG
DuncG

Reputation: 15196

When you find a command that works "from the terminal" but not from Java then you should assume that the PATH or other environment required with that command is only set correctly via that terminal, and not when called by JVM.

So, one possible fix is to run your terminal indirectly which may fix the environment needed - depending on your shell and it's setup:

    ProcessBuilder pb = new ProcessBuilder("bash", "-c", "tar --zstd -cf info-x.tar.zst tstpkg");

You might also try to fix by setting the correct PATH for running zstd as a sub-process of tar. Check where it is using which zstd and try adding to the PATH before pb.start():

String path = pb.environment().get("PATH"); // use Path on Windows
pb.environment().put("PATH", "/path/to/dir/ofzstd"+File.pathSeparator+path);

Upvotes: 0

Related Questions