title: Run SSH on SSH server using Java
body: <p>I want to open <code>ssh</code> by terminal with Java, JSch library and Ubuntu operating system, but there is error like this:</p>
  
  <pre class="lang-none prettyprint-override"><code>Pseudo-terminal will not be allocated because stdin is not a terminal.
  Permission denied, please try again.
  Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
  </code></pre>
  
  <p>This is my code</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  
  import com.jcraft.jsch.Channel;
  import com.jcraft.jsch.ChannelExec;
  import com.jcraft.jsch.JSch;
  import com.jcraft.jsch.Session;
  
  public class Basic {
      public static void main(String[] args) {
          String host = "dem.web.com";
          String user = "app";
          String password = "web";
          String command1 = "ssh app@web";
          try {
  
              java.util.Properties config = new java.util.Properties();
              config.put("StrictHostKeyChecking", "No");
              JSch jsch = new JSch();
              com.jcraft.jsch.Session session = jsch.getSession(user, host, 22);
              session.setPassword(password);
              session.setConfig(config);
              session.connect();
  
              Channel channel = session.openChannel("exec");
              ((ChannelExec) channel).setCommand(command1);
              channel.setInputStream(null);
              ((ChannelExec) channel).setErrStream(System.err);
  
              InputStream in = channel.getInputStream();
              channel.connect();
              byte[] 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));
                  }
                  if (channel.isClosed()) {
                      channel=session.openChannel("exec");
                      break;
                  }
                  try {
                      Thread.sleep(1000);
                  } catch (Exception ee) {
                  }
              }
              channel.disconnect();
              session.disconnect();
              System.out.println("DONE");
          } catch (Exception e) {
              e.printStackTrace();
          }
  
      }
  }
  </code></pre>
  
  <p>Is there someone who can help me? Or is there another way that easier?
  Thank you.</p>