0

I'm trying to run a linux command (with pipes)

using something like:

Runtime.getRuntime().exec("rpm -qa | grep "Uploader" | xargs rpm --queryformat "%{VERSION}" -q");

but i get as result only the output of the "rpm -qa"

can you help?

thanks

roman
  • 607
  • 2
  • 10
  • 18
  • You could try putting your command in a script and calling that. Java gets confused when there are multiple parameters. – Steve Smith Mar 07 '17 at 12:06
  • 1
    Neither java nor rpm know about pipes. Shell knows about pipes. So your best bet is to wrap your pipe in a small shell script and execute that! – Gyro Gearless Mar 07 '17 at 12:08
  • Possible duplicate of [Want to invoke a linux shell command from Java](http://stackoverflow.com/questions/1410741/want-to-invoke-a-linux-shell-command-from-java) – utsav_deep Mar 07 '17 at 12:34

1 Answers1

3

Only a shell understands pipes, you can invoke the shell with the command you want to run:

exec(new String[]{"/bin/sh", "-c", "rpm -qa | grep \"Uploader\" | xargs rpm --queryformat \"%{VERSION}\" -q"});
Magnus
  • 7,952
  • 2
  • 26
  • 52