开发者

Expectj - Getting everything that has been received on the spawn's stdout during this session

开发者 https://www.devze.com 2023-03-11 18:30 出处:网络
I am using Expectj 2.07. I am trying to use getCurrentStandardOutContents() to print everythin开发者_StackOverflowg that has been received on the spawn\'s stdout.

I am using Expectj 2.07. I am trying to use getCurrentStandardOutContents() to print everythin开发者_StackOverflowg that has been received on the spawn's stdout.

public class ExpectTest {

    public static void main(String args[])
    {
        ExpectJ expectInit = new ExpectJ(5);
        try
        {
            Spawn s = expectInit.spawn("/bin/sh");           
            s.send("echo debraj\n");            
            System.out.println("Output->"+s.getCurrentStandardOutContents());
            s.expectClose();

        }catch(Exception io)
        {
            io.printStackTrace();
        }


    }

}

But getCurrentStandardOutContents() is not showing anything.

OUTPUT:-

Output->

debraj


You may need to give the subprocess some time to work. Try adding a little bit of delay:

Spawn s = expectInit.spawn("/bin/sh");           
s.send("echo debraj\n");

Thread.sleep(200);                      // Pause for 0.2s

System.out.println("Output->"+s.getCurrentStandardOutContents());
s.expectClose();
0

精彩评论

暂无评论...
验证码 换一张
取 消