title: Running multiple unix commands through java | Jcraft - Jsch
body: <p>I am trying to run a series of unix commands through Java. Basically, whatever I do over Putty, I want to do it over java program.</p>
  
  <p>I have written 2 classes.</p>
  
  <ol>
  <li>Connects to the server and executes the Unix Command.</li>
  <li>Sends the unix commands to Class1 in a list.</li>
  </ol>
  
  <p>I am able to connect to server and execute, if there is only 1 value present at the list of Class2. However, when there are multiple values present in the list, the code executes only the latest command (value from the list) and skips all the other values.</p>
  
  <p>I want to execute each value (unix command) present on the list of Class2. Please help.
  I am using JCraft's JSch classes.</p>
  
  <p><strong>Class1</strong></p>
  
  <pre><code>package package1;
  
  import java.io.InputStream;
  import java.util.List;
  
  import com.jcraft.jsch.Channel;
  import com.jcraft.jsch.ChannelExec;
  import com.jcraft.jsch.JSch;
  import com.jcraft.jsch.Session;
  
  
  public class ConnExec 
  {
      static InputStream in;
      static byte[] tmp;
      static int flag = 0;
      public void connExec(List&lt;String&gt; commandLst)
      {
          String host="serverName.host.dev";
          String user="UserName";
          String password="PWD";
  
          try
          {
              java.util.Properties config = new java.util.Properties(); 
              config.put("StrictHostKeyChecking", "no");
              JSch jsch = new JSch();
              Session session=jsch.getSession(user, host, 22);
              session.setPassword(password);
              session.setConfig(config);
              session.connect();
              System.out.println("Connected to the server.....\n");
  
              Channel channel=session.openChannel("exec");
              channel.setInputStream(null);
  
              for (int x = 0; x &lt; commandLst.size();x++)
              {
                  ((ChannelExec)channel).setCommand(commandLst.get(x));
  
                  in=channel.getInputStream();
                  channel.connect();
                  tmp=new byte[1024];
  
                  while(true)
                  {
                      while(in.available()&gt;0)
                      {
                          int i=in.read(tmp, 0, 1024);
                          if(i&lt;0)break;
                          System.out.print(new String(tmp, 0, i));
                          System.out.println("\nExecuted.....");
                      }
  
                      if(channel.isClosed())
                      {
                          break;
                      }
                 }
              }
  
              channel.disconnect();
              session.disconnect();
              System.out.println("Terminated.....\n");
              flag = 1;
          }
          catch(Exception e)
          {
              e.printStackTrace();
              flag = 1;
          }
  
      }
  
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Class2</strong></p>
  
  <pre><code>package package1;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class ReadCommands 
  {
  
  
      public static void main(String a[])
      {
          List&lt;String&gt; lst = new ArrayList&lt;String&gt;();
  
          String command1="ls /local/dev/source/folder";
          String command2="ls /local/dev/source/folder/inbound";
  
          lst.add(command1);
          lst.add(command2);
  
          ConnExec ce = new ConnExec();
          ce.connExec(lst);
      }
  
  }
  </code></pre>