title: com.jcraft.jsch.JSchException: java.net.ConnectException: Connection refused: connect
body: <p>I understand that there is duplicate >>> copied from the duplicate >>>As long as your local machine has an SSH server running &lt;&lt;&lt;&lt;&lt; but i cannot comment and cannot as from the question (and im not providing an answer....)</p>
  
  <p>It stated that "As long as your local machine has an SSH server running" but I do not know how to have a SSh server running. I turn on my putty (double click on it) (not sure if this means the SSH(?Putty?) server (?) is running... doubt so... </p>
  
  <p>im really new to socket programming. i am making use of JSch (<a href="http://www.jcraft.com/jsch/" rel="noreferrer">http://www.jcraft.com/jsch/</a>) to try to connect to a remote server (later stage) 
  currently, this is the code i use and im trying to connect to my local computer and execute a command (ls to be exact) to do a test. However, i keep hitting the connection refused. I googled and i noticed that there are some articles that mentions on "having a server listening" but I have no idea what it means. Please view my code as below. </p>
  
  <pre><code>import java.awt.event.*;
  
  import javax.swing.*;
  
  import java.awt.*;
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.util.Properties;
  
  import com.jcraft.jsch.*;
  
  
  
  class SwingWorkerExample {
  
      JTextField hostField;
      JTextField userNameField;
      JTextField passwordField;
      JPanel panel;
  
  
      public SwingWorkerExample() {
          JPanel p = panel = new JPanel(new GridLayout(0,2));
          hostField = new JTextField(20);
          userNameField = new JTextField(20);
          passwordField = new JPasswordField(20);
          JButton testButton = new JButton("connect!");
          testButton.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent ev) {
                      testConnectionButtonActionPerformed(ev);
                  }
              });
          p.add(new JLabel("host:"));
          //127.0.0.1
          p.add(hostField);
          p.add(new JLabel("user:"));
          //mycomputerusername
          p.add(userNameField);
          p.add(new JLabel("password:"));
          //mycomputerpassword
          p.add(passwordField);
          p.add(testButton);
      }
  
      public JPanel getPanel() {
          return panel;
      }
  
      private void testConnectionButtonActionPerformed(ActionEvent evt) {
  
          SwingWorker sw = new SwingWorker(){
  
                  protected Object doInBackground() throws Exception {
                      try {
                          JSch jsch = new JSch();
  
                          String host = hostField.getText();
                          String username = userNameField.getText();
                          String password = passwordField.getText();
  
                          Session session = jsch.getSession(username, host);
                          session.setPassword(password);
                          session.setConfig("StrictHostKeyChecking", "no");
  
                          session.setTimeout(20000);
                          System.out.println("Connecting to server...");
                          session.connect();
  
                          return session;
                      }
                      catch(Exception ex) {
                          ex.printStackTrace();
                          throw ex;
                      }
                  }
  
                  public void done(){
                      try {
                          System.out.println(get());
                      } catch (Exception ex) {
                          ex.printStackTrace();
                      }
                  }
              };
  
          sw.execute();
  
      }
  
  
      public static void main(String[] egal) {
          EventQueue.invokeLater(new Runnable(){public void run() {
              SwingWorkerExample ex = new SwingWorkerExample();
              JFrame f = new JFrame("bla");
              f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
              f.setContentPane(ex.getPanel());
              f.pack();
              f.setVisible(true);
          }});
      }
  
      public void remoteLs() throws JSchException, IOException {
          JSch js = new JSch();
          Session s = js.getSession("kellyseo", "192.168.0.103", 22);
          s.setPassword("S9031808z");
          Properties config = new Properties();
          config.put("StrictHostKeyChecking", "no");
          s.setConfig(config);
          s.connect();
  
          Channel c = s.openChannel("exec");
          ChannelExec ce = (ChannelExec) c;
  
          ce.setCommand("ls -l");
          ce.setErrStream(System.err);
  
          ce.connect();
  
          BufferedReader reader = new BufferedReader(new InputStreamReader(ce.getInputStream()));
          String line;
          while ((line = reader.readLine()) != null) {
            System.out.println(line);
          }
  
          ce.disconnect();
          s.disconnect();
  
          System.out.println("Exit code: " + ce.getExitStatus());
  
        }
  
  
  
        public void remoteMkdir() throws JSchException, IOException {
          JSch js = new JSch();
          Session s = js.getSession("myusername", "myremotemachine.mycompany.com", 22);
          s.setPassword("mypassword");
          Properties config = new Properties();
          config.put("StrictHostKeyChecking", "no");
          s.setConfig(config);
          s.connect();
  
          Channel c = s.openChannel("exec");
          ChannelExec ce = (ChannelExec) c;
  
          ce.setCommand("mkdir remotetestdir");
          ce.setErrStream(System.err);
  
          ce.connect();
  
          BufferedReader reader = new BufferedReader(new InputStreamReader(ce.getInputStream()));
          String line;
          while ((line = reader.readLine()) != null) {
            System.out.println(line);
          }
  
          ce.disconnect();
          s.disconnect();
  
          System.out.println("Exit code: " + ce.getExitStatus());
  
        }
  
        public void remoteCopy() throws JSchException, IOException, SftpException {
          JSch js = new JSch();
          Session s = js.getSession("myusername", "myremotemachine.mycompany.com", 22);
          s.setPassword("mypassword");
          Properties config = new Properties();
          config.put("StrictHostKeyChecking", "no");
          s.setConfig(config);
          s.connect();
  
          Channel c = s.openChannel("sftp");
          ChannelSftp ce = (ChannelSftp) c;
  
          ce.connect();
  
          ce.put("/home/myuser/test.txt","test.txt");
  
          ce.disconnect();
          s.disconnect();    
        }
  }
  </code></pre>
  
  <p>BTW i uses commandprompt to ping 127.0.0.1 is okay but if i use telnet 127.0.0.1 it says Could not open connection to the host (I turned on putty(?double click?) , on port 23: Connect failed.
  and, SSH = PUTTY ... right? (i cannot use the 'ssh' command in command prompt)</p>
  
  <p>Links:
  1) <a href="http://sourceforge.net/p/jsch/mailman/message/31745775/" rel="noreferrer">http://sourceforge.net/p/jsch/mailman/message/31745775/</a></p>
  
  <p>and 2) <a href="http://javarevisited.blogspot.sg/2013/02/java-net-ConnectException-Connection-refused.html" rel="noreferrer">http://javarevisited.blogspot.sg/2013/02/java-net-ConnectException-Connection-refused.html</a></p>
  
  <p>and 3) <a href="http://www.jcraft.com/jsch/examples/" rel="noreferrer">http://www.jcraft.com/jsch/examples/</a>
   and 4) <a href="https://stackoverflow.com/questions/2405885/any-good-jsch-examples">Run a command over SSH with JSch</a>
   and 5) <a href="https://stackoverflow.com/questions/4932005/can-we-use-jsch-for-ssh-key-based-communication">Can we use JSch for SSH key-based communication?</a></p>
  
  <p>and... thanks is advance!!</p>
  
  <p>oh, and theres also <a href="http://www.ganymed.ethz.ch/ssh2/" rel="noreferrer">http://www.ganymed.ethz.ch/ssh2/</a>   (an alternative to JSch.. any advise welcomed!) but when i try to run the example, it says no main. which.. i duno >.&lt; Will stick w JSch till then....</p>
  
  <p>BTW, i try <a href="https://serverfault.com/questions/185153/free-public-ssh-server-for-testing-purposes">https://serverfault.com/questions/185153/free-public-ssh-server-for-testing-purposes</a> for server but... i have no idea whats the address, username and password. (i also have a <a href="http://sdf.org" rel="noreferrer">http://sdf.org</a> account <em>newly created</em> but when i try to connect to it, it says unknownhost. fyi!)</p>
  
  <p>forgot to mention, im using windows 7 and 'yum' is not a command in my command prompt...</p>