使用Jcraft JSch连接到远程服务器的java SWT

时间:2015-08-24 01:49:50

标签: java swt bufferedreader jsch system.in

我正在开发一个工具,使用 jcraft.JSch (ssh到Unix服务器)连接到远程服务器并返回输出并显示其输出(类似于putty,但我不热衷在Plink或任何第三方的事情)。当通道输入输出为System.inSystem.out且程序只是一个控制台java应用程序时,代码工作正常。但是,当我尝试使用SWT控件时,遇到问题,如How to Map System.out and System.in to SWT controls

中所述

所以,我相信它有关于INFO和ERR标记的东西(我将在稍后处理),无论如何我改变了方法。现在iam通过SWT文本输入提供命令。它适用于单线输出。但不是多线。

enter image description here

触发与shell连接并发送命令的代码如下:

        public void widgetSelected(SelectionEvent e) {
        String command=CmdText.getText();
        System.out.println("runButton.widgetSelected:Command Obtained by buton is:"+command);
        if ( command.equals("") )
        {
           MessageDialog.openInformation(dialogShell,"Information","Please Eneter a Command!");
           return;
        }
        else if (command.contains("@"))
        {
            channel=CommandHandler.openshell();
            LogText.append("Connected to - :"+command+"\n>");
        }
        else
        {
            LogText.append(command+"\n");
            String outputstring="";
            try
            {
                 BufferedReader dataIn = new BufferedReader(new InputStreamReader(channel.getInputStream()));
                 DataOutputStream dataOut = new DataOutputStream(channel.getOutputStream());  

                 dataOut.writeBytes(command+"\n");
                 dataOut.flush();

                 String line="";
                 outputstring=""+line;
                 while ( line != null) {
                     try
                     {
                        line = dataIn.readLine();
                        line = dataIn.readLine();
                        LogText.append("<CommandResult>:"+line+"\n");
                        return;
                     }
                     catch (IOException e1) { B24IDEConsole.WriteLog("RemoteCall.connect_execute", e1.getMessage()); }
                     outputstring=outputstring+line;
                 }
            }
            catch(Exception e1){ B24IDEConsole.WriteLog("RemoteCall.connect_execute", e1.getMessage()); }
            LogText.append("<EndOfCommand>:"+outputstring+"\n");
        }
    }

当我在while循环中删除返回时,对话框和 eclipse (触发对话框)变得无响应,我必须同时杀死它们。

感谢您的帮助,谢谢您的时间。

0 个答案:

没有答案