title: run multiple commands on remote server
body: <p>So far I've managed to connect, run a single command and then disconnect. The problem I'm having is running a second, third, etc command thereafter.</p>
  
  <pre><code>public static void main(String args[]) {
          try {
              JSch js = new JSch();
              Session session = js.getSession("myuser", "myhost", 22);
              session.setPassword("mypassword");
              Properties properties = new Properties() {
                  {
                      put("StrictHostKeyChecking", "no");
                  }
              };
              session.setConfig(properties);
              session.connect();
  
              Channel channel = session.openChannel("exec");
  
              ChannelExec channelExec = (ChannelExec) channel;
              channelExec.setCommand("ls");
              channelExec.setErrStream(System.err);
              channelExec.connect();
  
              BufferedReader reader = new BufferedReader(new InputStreamReader(channelExec.getInputStream()));
              String line;
              while ((line = reader.readLine()) != null) {
                  System.out.println(line);
              }
  
  //            This part doesn't work. It causes the program to hang.
  //            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(channelExec.getOutputStream()));
  //            writer.write("cd Downloads");
  //            writer.write("ls");
  //            reader = new BufferedReader(new InputStreamReader(channelExec.getInputStream()));
  //            while ((line = reader.readLine()) != null) {
  //                System.out.println(line);
  //            }
  
              channelExec.disconnect();
              session.disconnect();
  
              System.out.println("Exit code: " + channelExec.getExitStatus());
          } catch (JSchException | IOException ex) {
              Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  </code></pre>
  
  <p>I've tried using the <code>ChannelExec</code> <code>OutputStream</code> but that just caused the program to do nothing so I suspect that's not the way to go.</p>
  
  <p>What should I add to print the contents following the commands <code>cd Downloads</code> and <code>ls</code> for example after the first <code>ls</code> output has been printed?</p>