When i apply command command i do not get any output. How can i get output while using command or not both case?
public class test
{
public static void main(String args[])
{
System.out.println(
systemtest("ifconfig | awk 'BEGIN { FS = \"\n\"; RS = \"\" } { print $1 $2 }' | sed -e 's/ .*inet addr:/,/' -e 's/ .*//'"));
}
public static String systemtest(String cmds)
{
String value = "";
try
{
String cmd[] = {
"/bin/sh",
"-c",
cmds
};
Process p=Runtime.getRuntime().exec(cmd);
// Try 0: here? wrong
//p.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
String line=reader.readLine();
// Try 1: here?
//p.waitFor();
while(line!=null)
{
value += line + "\n";
line=reader.readLine();
}
// Try 2: here?
p.waitFor();
} catch(IOException e1) {
} catch(InterruptedException e2) {
}
return value;
}
You need to read the output from the command before you call
p.waitFor().Facts:
It is not hard to see (from the above facts) that the way that you’re application is written will result in a deadlock if the external application produces more output than can fit into the pipe’s buffer (in O/S space).
(Even if this is not the real cause of your problem this time, it could be other times. Watch out for deadlocks when reading from / writing to external processes.)