-1

hello guys i am working on program in which i have to perform a certain task after which i can close the window also.. obviously but it is not closing the window... my main class is like

public class laudit {
  public static void main(String[] arg){

    SwingUtilities.invokeLater( new Runnable(){
        public void run(){
        JFrame frame = new mainFrame("Linux Audit");
        frame.setVisible(true);
        frame.setSize(700,600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    });

  }
}

Second class with panel is...following in which i am starting a ssh connection but it is not stoping as it is supposed to after completion..

public class panel extends JPanel {

 String shost;
 String suser;
 String spass;
 int sport;
 public int getsport() {
       return this.sport;
    }
 public String getshost() {
       return this.shost;
    }
 public String getsuser() {
       return this.suser;
    }
 public String getspass() {
       return this.spass;
    }
 public panel(){
    Dimension size = getPreferredSize();
    size.width = 680;
    size.height = 600;
    setPreferredSize(size);
    setBorder(BorderFactory.createTitledBorder("Linux Audit"));
    setLayout(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints();


    JLabel labelhost = new JLabel("Host    ");
    JLabel labeluser = new JLabel("User name    ");
    JLabel labelpass = new JLabel("Password    ");
    JLabel labelport = new JLabel("Port    ");
    final JTextField host = new JTextField(15);
    final JTextField user = new JTextField(15);
    final JTextField pass=(JTextField)new JPasswordField(15);
    final JTextField port = new JTextField(15);
    final JButton start = new JButton("Start Audit");
    //layout design
    gc.anchor = GridBagConstraints.LINE_END;
    gc.weightx = 0.5;
    gc.weighty = 0.5;
    gc.gridx=0;
    gc.gridy=0;
    add(labelhost,gc);
    gc.gridx=0;
    gc.gridy=1;
    add(labeluser,gc);
    gc.gridx=0;
    gc.gridy=2;
    add(labelpass,gc);
    gc.gridx=0;
    gc.gridy=3;
    add(labelport,gc);
    gc.anchor = GridBagConstraints.LINE_START;
    gc.gridx=1;
    gc.gridy=0;
    add(host,gc);
    gc.gridx=1;
    gc.gridy=1;
    add(user,gc);
    gc.gridx=1;
    gc.gridy=2;
    add(pass,gc);
    gc.gridx=1;
    gc.gridy=3;
    add(port,gc);
    gc.anchor = GridBagConstraints.FIRST_LINE_START;
    gc.weighty=10;
    gc.gridx=1;
    gc.gridy=4;
    add(start,gc);


start.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {

            String shost = host.getText();
            String suser = user.getText();
            String spass = pass.getText();
            String sportb = port.getText();
            int sport = Integer.parseInt(sportb);

            sshConnection s = new sshConnection();
            try {
                s.Connection(shost,suser,spass,sport);
            } catch (JSchException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }


        }


    });

here is the ssh connection i am very new to java programming

public class sshConnection {

    public void Connection (String sHost,String sUser,String sPass,int sPort)throws     JSchException, IOException{

         String sshhost = sHost;
         String sshuser = sUser;
         String sshpass = sPass;
         int sshport = sPort;
         /*System.out.println(sshhost);
         System.out.println(sshuser);
         System.out.println(sshport);
         System.out.println(sshpass);*/
         String endLineStr = " # ";
         JSch shell = new JSch();  
         // get a new session    
         Session session = shell.getSession(sshuser, sshhost, sshport);  

            // set user password and connect to a channel  
            session.setUserInfo(new SSHUserInfo(sshpass));  
            session.connect();  
            Channel channel = session.openChannel("shell");  
            channel.connect();  

            DataInputStream dataIn = new DataInputStream(channel.getInputStream());  
            DataOutputStream dataOut = new DataOutputStream(channel.getOutputStream());  
          //file start
            File f = new File("Result.txt");
            if(!f.exists())
            {
              try {
                         f.createNewFile();
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
            }

          try {
                  FileOutputStream fos = new FileOutputStream(f);
                  PrintStream ps = new PrintStream(fos);
                  System.setOut(ps);
          } catch (Exception e) {
              e.printStackTrace();
          } //file end

            // send ls command to the server  
            dataOut.writeBytes("ls -la\r\n");  
            dataOut.flush();  

            // and print the response   
            String line = dataIn.readLine();  
            System.out.println(line);  
            while(!line.endsWith(endLineStr)) {  
                System.out.println(line);  
                line = dataIn.readLine();  
            }  
            dataIn.close();  
            dataOut.close();  
            channel.disconnect();  
            session.disconnect();  
        }  }

    class SSHUserInfo implements UserInfo {  
    private String sshpass;  

    SSHUserInfo(String sshpass) {  
        this.sshpass = sshpass;  
    }  

    public String getPassphrase() {  
        return null;  
    }  

    public String getPassword() {  
        return sshpass;  
    }  

    public boolean promptPassword(String arg0) {  
        return true;  
    }  

    public boolean promptPassphrase(String arg0) {  
        return true;  
    }  

    public boolean promptYesNo(String arg0) {  
        return true;  
    }  

    public void showMessage(String arg0) {  
        System.out.println(arg0);  
    }  
}  

Please help me out everything is working fine except this flaw... Thanks..

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 2
    I would hesitate a guess that the `sshConnection#Connection` method is preventing the main thread from being terminated. Try placing it in a daemon thread and see if it makes a difference – MadProgrammer Sep 16 '13 at 23:04
  • I never used invokeLater, is there something I am missing with swing? I always call the jframe directly in the main, am I wrong? – Gianmarco Sep 16 '13 at 23:21
  • 2
    @Gianmarco, ' am I wrong?' - Yes. All Swing code should execute on the EDT. Read the section from the Swing tutorial on [Concurrency](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) for more information. – camickr Sep 17 '13 at 01:02
  • it will be more helpful if anybody can send some snippet for the fix. Thanks....................like @madprogrammer you said – user2761414 Sep 17 '13 at 04:29
  • it will be then more helpful if you give us your salary. – Jeremy D Sep 17 '13 at 06:18
  • Still in college trying to learn new things...Thanks for the nice comment..but it was total waste and useless – user2761414 Sep 17 '13 at 06:27
  • So, a few things you want to know then ;) - [Code Conventions for the Java Programming Language](http://www.oracle.com/technetwork/java/codeconv-138413.html); Use meaningful class names (`panel` is not a meaningful class name); Don't hijack the `stdout` without 1- Providing a means to echo it through it's original references and 2- resetting it when you're done. – MadProgrammer Sep 17 '13 at 07:05

1 Answers1

1

The most likely cause of your problem is to do with sshConnection#Connection. This is likely blocking the Event Dispatching Thread, preventing it from processing any new events, like the window closing event.

You need to overload the connection to a seperate thread, for example...

start.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        String shost = host.getText();
        String suser = user.getText();
        String spass = pass.getText();
        String sportb = port.getText();
        int sport = Integer.parseInt(sportb);

        SSHTask task = new SSTask(shost, suser, spass, sport);
        Thread thread = new Thread(task);
        thread.start();
    }
});

SSHTask class

public class SSHTask implements Runnable {
    private String host;
    private String user;
    private String pass;
    private int port;

    public SSHTask(String host, String user, String pass, int port) {
        this.host = host;
        this.user = user;
        this.pass = pass;
        this.port = port;
    }

    public void run() {
        sshConnection s = new sshConnection();
        try {
            s.Connection(host,user,pass,port);
        } catch (JSchException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}

Take a look at Concurrency for more details.

If you are going to need to interact with the UI from this Thread, you would be better of using a SwingWorker, see Concurrency in Swing for details

Updated

If you still have problems. One other thing you could try is make the Thread a daemon thread...

SSHTask task = new SSTask(shost, suser, spass, sport);
Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();

This is a little harsh and there is still no guarantee that the system will exit while the you have a connection open...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • programmer i tried your way but not working for me as i am newbie...plz explain it in keeping that in mind please thanks i am sending you the sshnnConnection class please help – user2761414 Sep 17 '13 at 05:51
  • I tested the code it seems to work fine for me. If you still have problems, then you may need to look into ways to force a disconnect – MadProgrammer Sep 17 '13 at 07:06