【发布时间】:2016-09-17 22:12:10
【问题描述】:
我正在尝试创建一个连接到 Linux 虚拟机(使用JSch)的应用程序,并向 Linux 询问一些关于自身的问题,例如操作系统名称和内核版本。我已经成功了,并且应用程序可以工作..但只能在 Eclipse 控制台中。
如果我尝试将其打印在标签或 TextArea 上...奇怪的事情正在发生。例如,如果我尝试在标签上打印出来,那么它只会打印出最后一个命令。如果我用 TextArea 尝试它,那么它会打印出所有内容,但在一行中,我不知道如何停止这些线......
代码如下:
import javafx.fxml.FXML;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class MainWindowController {
@FXML private TextField ip_text_field, username_text_field, password_text_field;
@FXML private Label output;
String ip, username, pass;
private Main main;
public void setMain(Main main){
this.main = main;
}
public String getIP(){ip = ip_text_field.getText(); return ip;}
public String getUsername(){username = username_text_field.getText(); return username;}
public String getPassword(){pass = password_text_field.getText(); return pass;}
public void connectButtonFunction(){
try{
String command = "lsb_release -a | grep -i Description && uname -mrs";
String host = getIP();
String user = getUsername();
String password = getPassword();
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);;
session.setPassword(password);
session.connect();
Channel channel = session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream input = channel.getInputStream();
channel.connect();
//System.out.println("Channel Connected to machine " + host + " server with command: " + command );
try{
InputStreamReader inputReader = new InputStreamReader(input);
BufferedReader bufferedReader = new BufferedReader(inputReader);
String line = null;
while((line = bufferedReader.readLine()) != null){
//System.out.println(line);
output.setText(line);
}
bufferedReader.close();
inputReader.close();
}catch(IOException ex){
ex.printStackTrace();
}
channel.disconnect();
session.disconnect();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
这是标签的样子。
【问题讨论】: