Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6212111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:27:25+00:00 2026-05-24T06:27:25+00:00

I’m trying to connect to SSH Unix server on button click (code written in

  • 0

I’m trying to connect to SSH Unix server on button click (code written in actionPerformed() method). I’m using JSch for connecting to SSH server. The code is written in SwingWorker class as it is a network operation.

private void testConnectionButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                     

        SwingWorker<Boolean, Void> sw = new SwingWorker<Boolean, Void>(){

            @Override
            protected Boolean doInBackground() throws Exception {
                JSch jsch = new JSch();

                String host = "ServerHost";
                String username = "username";
                String password = "password";

                Session session = jsch.getSession(username, host);
                session.setPassword(password);

                session.setTimeout(20000);
                System.out.println("Connecting to server...");
                session.connect();

                return true;
            }

            @Override
            public void done(){
                try {
                    System.out.println(get().toString());
                } catch (Exception ex) {
                    System.out.err(ex);
                } 
            }
        };

        sw.execute();

    }  

But after running the with correct host, username and password details, I get the below error all the time:

com.jcraft.jsch.JSchException: timeout: socket is not established
    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:222)
    at java.util.concurrent.FutureTask.get(FutureTask.java:83)
    at javax.swing.SwingWorker.get(SwingWorker.java:583)

But whenever I run the same code in standalone program, I mean instead for writing actionPerformed() method, If I write it in normal method and calling from main() method. It will work. when I integrate the same code with Button Click’s actionPerformed() method, it will give me above exception.

Can anyone suggest what I’m doing wrong here or any modification should be made to the code.

I tried to connect to SSH Server using “SSHJ” implementation, but I get the below error:

java.net.SocketException: Connection reset
    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:222)
    at java.util.concurrent.FutureTask.get(FutureTask.java:83)
    at javax.swing.SwingWorker.get(SwingWorker.java:583)

Can someone help me – how to move forward?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:27:25+00:00Added an answer on May 24, 2026 at 6:27 am

    I took your code, wrapped it in some GUI code (and converted it to non-generics to be able to compile it with the same settings as the rest of the JSch examples). It works for me. Try this, and report what exception you get (it has a bit more exception logging).

    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    
    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:"));
            p.add(hostField);
            p.add(new JLabel("user:"));
            p.add(userNameField);
            p.add(new JLabel("password:"));
            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);
            }});
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.