0

I try to run the following command to get only a line of memory status.

Runtime.getRuntime().exec("free -m | grep 'Mem'");

But it returns all lines of result. The command after the vertical bar is not processed. I also try:

Runtime.getRuntime().exec(new String[] { "free", "-m", "|", "grep", "Mem" });

And it doesn't work either.

Purres
  • 1,271
  • 2
  • 11
  • 12

1 Answers1

1

Piping with the "|" is only possible in a shell, but if you do runtime.exec() you have no shell. It just executes the other process. Therefore the pipe gets simply ignored. In your case, you could solve this relatively simple by using a regex matcher in your java program to extract the results.

lwi
  • 1,682
  • 12
  • 21