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

3
  • You could try putting your command in a script and calling that. Java gets confused when there are multiple parameters. Commented Mar 7, 2017 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! Commented Mar 7, 2017 at 12:08
  • Possible duplicate of Want to invoke a linux shell command from Java Commented Mar 7, 2017 at 12:34

1 Answer 1

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

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.