I have a shell script that has a single command to generate a file with the required information in it after sucessful creation of the script.
Now when Iam executing that command directly or executing the script itself from the cmd line the file is getting generated.
But when iam trying to execute the same script from the java code using Runtime.getRuntime().exec(cmd)
the file is not getting generated.The traces just before and after this line are displayed as expected indicated that the execution of the script is not throwing any exceptions.
Where am I going wrong or what am I missing on? Plz help!!!
Example this is the code iam tryng to execute
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Scripttest {
public static void main(String args[])
{
try{
String cmd="ls|grep sys";
Process p=Runtime.getRuntime().exec(cmd);
System.out.println("done");
final BufferedReader input = new BufferedReader(new InputStreamReader( p.getInputStream()));
String line = null;
while ((line = input.readLine()) != null)
System.out.println("proc: " + line);
}
catch(Exception e)
{
System.out.println("Exception is"+e);
}
}
}
and the output iam getting is this
java Scripttest
Exception isjava.io.IOException: Cannot run program "ls|grep": error=2, No such file or directory
The cmd is not getting executed but not sure abt the mistake why it is not.