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.

5
  • 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 Commented Nov 12, 2017 at 9: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. Commented Nov 12, 2017 at 9: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 Commented Nov 12, 2017 at 9: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: stackoverflow.com/a/16983563/829571 Commented Nov 12, 2017 at 10:22
  • 1
    Hello assylias, thanks a lot for the info Commented Nov 12, 2017 at 18:46

2 Answers 2

9

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", ".*"); 
Sign up to request clarification or add additional context in comments.

Comments

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 ");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.