Search code examples
javalinuxprocessbuilder

Can't run program with ProcessBuilder, runs fine from command line


On linux (debian), I can run this command:

/usr/lib/jvm/jdk1.7.0_21/bin/java -jar ~/myjar.jar ".*"

I am trying to run it from a Java program instead with:

ProcessBuilder pb = new ProcessBuilder(java, "-jar", "~/myjar.jar", "\".*\"");

System.out.println(pb.command()); prints the following, as expected:

[/usr/lib/jvm/jdk1.7.0_21/bin/java, -jar, ~/myjar.jar, ".*"]

However I don't get the same output from the program (it runs but the ouput looks as if the ".*" argument is not properly taken into account).

Any ideas why it doesn't work?

Note: the same code works fine on Windows.


Solution

  • Looks like the wildcard character is not being expanded using a glob. You can use a shell instead:

    ProcessBuilder pb = 
           new ProcessBuilder("bash", "-c", "java -jar ~/myjar.jar \".*\"");
    

    or you can remove the double-quotes around the wildcard:

    ProcessBuilder pb = new ProcessBuilder(java, "-jar", "~/myjar.jar", ".*");