Search code examples
javaprocessbuilder

Java ProcessBuilder command with asterisk


This command runs from the command line successfully :

// /home/[user]/testproject/target/dependency/testproj-1.0-SNAPSHOT.jar
~/testproject/target/dependency$ jar -xf *.jar

But when I try to run the same command via ProcessBuilder I get :

java.io.FileNotFoundException: *.jar (No such file or directory)

ProcessBuilder code:

ProcessBuilder builder = new ProcessBuilder("jar", "-xf", "*.jar");
builder.directory("/home/[user]/testproject/target/dependency");
Process process = builder.start();
int exitValue = process.waitFor();

How can I make it work with the "*" among the command parameters?


Solution

  • From the command line the shell is performing globbing, converting *.jar to a list of actual files all ending with .jar.

    When run via ProcessBuilder no shell is involved. You must either:

    1. Run this in a shell. I.e. the command would be /bin/bash or whatever shell you use and the jar command would be the first argument to the shell.
    2. Perform the globbing yourself and specify the list of jars