Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 332
assylias Avatar asked Jun 01 '26 06:06

assylias


1 Answers

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", ".*");
like image 57
Reimeus Avatar answered Jun 04 '26 00:06

Reimeus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!