-1

大家好,我正在开发程序,在该程序中我必须执行某个任务,之后我也可以关闭窗口..显然但它没有关闭窗口......我的主要课程就像

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);
        }
    });

  }
}

带面板的第二类是...在其中我开始一个 ssh 连接但它并没有像完成后应该停止的那样停止..

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();
            }


        }


    });

这是 ssh 连接我对 java 编程很陌生

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);  
    }  
}  

请帮助我一切正常,除了这个缺陷......谢谢..

4

1 回答 1

1

您的问题最可能的原因是与sshConnection#Connection. 这可能会阻塞事件调度线程,阻止它处理任何新事件,例如窗口关闭事件。

您需要将连接重载到单独的线程,例如...

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班级

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();
        }
    }
}

查看并发以获取更多详细信息。

如果您需要从这里与 UI 交互您最好Thread使用SwingWorker

更新

如果你还有问题。您可以尝试的另一件事是创建Thread一个守护线程...

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

这有点苛刻,仍然不能保证系统会在您打开连接时退出......

于 2013-09-17T04:37:52.293 回答