public class LinuxInteractor {
public static String executeCommand(String command)
{
System.out.println("Linux command: " + command);
try
{
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader bf=new BufferedReader(new InputStreamReader( p.getInputStream()));
String str=bf.readLine();
System.out.println("inputStream is::"+str);
while( (str=bf.readLine()) != null)
{
System.out.println("input stream is::"+str);
}
System.out.println("process started");
}
catch (Exception e) {
System.out.println("Error occured while executing Linux command. Error Description: "
+ e.getMessage());
e.printStackTrace();
}
}
When I run the script through console, it's working. But through Java program InputStream(Str) is coming as null.
Is there any other approach I can use?
execand build theProcessusing aProcessBuilder. Also break aString argintoString[] argsto account for arguments which themselves contain spaces. 2) I suspect thewaitForcall should be after the stream consumption.InputStream(Str)in your code, but if you are claiming thatProcess.getInputStream()returns null it's hard to believe that you are correct.InputStreamis null or it isn't. If it's null you can't 'read it throughBufferedReader' at all. Do you mean you read a null withBufferedReader.readLine()?