I would like a help from u all .I am using java and i want to execute the a command that is use to get the channels details in asterisk. the system is linux based.
I want to execute command “core show channels” at the same time want to grep channels also
Following is the command and the output when fire normally in console.
asterisk -vvvvvrx 'core show channels' | grep channels
and the output is
2 active channels
I am trying to use following code in java
import java.io.IOException;
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ProcessBuilderExample
{
public static void main(String[] args)
throws IOException, InterruptedException
{
String[] commands = new String[]{"asterisk","-rx","core show channels","| grep 'channels'"};
Process p = Runtime.getRuntime().exec(commands);
p.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while((line = br.readLine()) != null) {
System.out.println(line);
}
System.out.println("hello");
}
}
but it is not showing me the correct output pls help me to solve this problem
Try to change
String[] commands = new String[]{"asterisk","-rx","core show channels","| grep 'channels'"};to:
Reason:
Your vertical bar will be interpreted as “\|”(i.e. literally “|” instead of a pipe”), if we instead invoke a shell command(/bin/sh) and put the whole command as its argument, vertical bar will be correctly interpreted.