3

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.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • Do not Use ProcessBuilder heavily, because it is not stable, very easily to fail for command with complex output like 'mvn build'. Use the shell script instead in case possible – Happy Nov 12 '17 at 09:15
  • @Amos I regularly use ProcessBuilder for commands with large output without any problems, as long as the process stream is consumed - I don't really know what stability issues you are referring to. – assylias Nov 12 '17 at 09:22
  • Hello assylias , Earlier we were using ProcessBuilder to run mvn (maven) commands, and it is usually get stuck and the ProcessBuilder stops there forever – Happy Nov 12 '17 at 09:26
  • 2
    @Amos the likeliest reason is that you are not consuming the output stream of the Process in a timely fashion. See for example: https://stackoverflow.com/a/16983563/829571 – assylias Nov 12 '17 at 10:22
  • 1
    Hello assylias, thanks a lot for the info – Happy Nov 12 '17 at 18:46

2 Answers2

8

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", ".*");
Reimeus
  • 158,255
  • 15
  • 216
  • 276
0

For. e.g. to get all the cpu frequency in Android.

ProcessBuilder p = new ProcessBuilder(); p.command("/system/bin/sh","-c","cat sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq ");

Alok Prasad
  • 622
  • 7
  • 12