I have the following code skeleton
try {
...
...
sysout("Please provide an input: ");
Thread.sleep(10000); //10 secs time given to the user to give the input. If the user doesn't enter the input it is throwing the exception, which is not being handled (with the custom message)
interact(); //This is a method that belongs to **expectj.Spawn** class
...
...
} catch (Exception e) {
System.out.println("You have not given any input!Please try again!");
}
But i still get the following output-
Exception in thread "ExpectJ Stream Piper" java.nio.channels.IllegalBlockingModeException
at sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:39)
at sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:92)
a开发者_开发技巧t sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:86)
at java.io.InputStream.read(InputStream.java:85)
at expectj.StreamPiper.run(StreamPiper.java:134)
Is there any other kind of exception i need to handle?
No, IllegalBlockingModeException
is a subclass of Exception
(a couple of levels down), so you are catching the right type. See the javadoc.
However, it might be that the exception is being thrown from a different thread, in which case you would not see it in your try/catch block. The thread which has the exception thrown is "ExpectJ Stream Piper"
.
There was no luck trying to get rid of the exception.So, instead of interact() we can use bufferedreader to take the input into a string and pass that string into send(). That is more convenient.
I think scanner is better than BufferedReader.
ExpectJ exp = new ExpectJ(10);
String cmd = "sh test.sh";
Scanner sc = new Scanner();
Spawn s = exp.spawn(cmd);
s.expect("Name");
String answer1 = sc.next();
s.send(answer1 + "\n);
s.expect("Password");
String answer2 = sc.next();
s.send(answer2+"\n");
.
.
.
//and so on...
精彩评论