If I call a external command from a Java program, How can I catch prompt message like the 2nd line below in both windows & linux
>sftp abc@abc.com
sftp>
I already tried below code , but neither InputStream nor ErrorStream can get that prompt message:
private static class PReader extends Thread{
private final InputStream i;
private final String label;
PReader(InputStream i ,String label){
this.i = i ;
this.label = label;
}
public void run(){
int w ;
int p = 0 ;
try {
while((w=i.read())!=-1){
if(c++ == 0 || '\n' == (char)p) {
System.out.print(label);
}
System.out.print((char)w);
p =w ;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("/usr/bin/sftp", "abc@abc.com");
Process p = pb.start();
new PReader(p.getInputStream(),"OUT:").start();
new PReader(p.getErrorStream(),"ERR:").start();
p.waitFor();
}
Edit: I edited the post so now it's not about asking for password . And I can get that prompt message by redirecting stdout from shell, but still can't get it by java Process
>sftp 1>a.log 2>b.log
>cat a.log
sftp>