I am using JSch to run a sudo command on a remote linux server.
The example below shows an attempt to cd into a directory and return the contents. I am testing the use case where myDirectory does NOT exist.
public static List<String> executeExecCommand() {
  try {
    Session session = new JSch().getSession() // paraphrasing for brevity
    Channel channel - session.openChannel("exec");
    String command = "echo \"password\" | sudo -S bash -c \"cd myDirectory/ && ls -la \"";
    ((ChannelExec(channel).setCommand(command);
    channel.setInputStream(null);
    ((ChannelExec(channel).setErrStream(System.err);
    InputStream input = channel.getInputStream();
    channel.connect();
    List<String> output = new ArrayList<>();
    try {
        InputStreamReader inputReader = new InputStreamReader(input);
        BufferedReader bufferedReader = new BufferedReader(InputReader);
        String line = null;
        while (true) {
            while ((line = bufferedReader.readLine()) != null) {
                output.add(line);
                System.out.println("here is a line " + line);
            }
        if (channel.isClosed()( {
            if (input.available() > 0) {
                continue;
            }
            logger.DEBUG("exit status " + channel.getExitStatus());
            break;
        }
        bufferedReader.close();
        inputReader.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    channel.disconnect();
    session.disconnect();
    return output;
    } catch (Throwable t) {
        throw new JSchException("executeExecCommand : unable to establish SSH session " + t.toString());
    }
}
When run from a Unit Test in intelliJ I get output as follows:
.
.
[sudo] password for username : bash: line 0: cd: myDirectory/: No such file or directory
2019-01-03 10:40:18 DEBUG [main] <Filename>:<linenumber> - exit status: 1
.
.
This is to be expected, but my question is How do I programmatically access the [sudo] output line? I have looked at the ErrorStream but that does not contain the text which is output and displayed on intelliJ's output console.
Note the line System.out.println("here is a line " + line); is not executed.
Note also that that the [sudo] line is not output when run from command prompt : ie "mvn test"
So, how do I programmatically access the [sudo] output line?