【发布时间】:2016-01-09 02:22:34
【问题描述】:
您好,我正在使用 Jsch 运行我的 shell 命令。我想让它具有交互性。我正在使用 java textarea 来显示脚本的输出。这个脚本也需要一些用户输入。 我怎样才能让这个文本区域接受用户输入并运行命令。
public void init(){
try{
JSch jsch=new JSch();
Panel panel = new Panel();
TextArea log = new TextArea();
panel.add(log);
add(panel);
PrintStream printStream = new PrintStream(new CustomOutputStream(log));
System.setOut(printStream);
System.setErr(printStream);
Session session=jsch.getSession("akumar", "banas", 22);
String passwd = "*****";
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword(passwd);
session.connect(30000); // making a connection with timeout.
Channel channel=session.openChannel("shell");
channel.setInputStream(System.in);
channel.setOutputStream(System.out);
channel.connect(3*1000);
printStream.println("sftp akumar@banas");
printStream.flush();
}
catch(Exception e){
System.out.println(e);
}
}
// This is the code for making textarea as output stream
public class CustomOutputStream extends OutputStream {
private TextArea textArea;
public CustomOutputStream(TextArea textArea) {
this.textArea = textArea;
}
@Override
public void write(int b) throws IOException {
// redirects data to the text area
textArea.append(String.valueOf((char)b));
// scrolls the text area to the end of data
textArea.setCaretPosition(textArea.getText().length());
}
}
【问题讨论】:
-
您可以在此处找到 TextAreaOutputStream 和 TextAreaReader:sourceforge.net/p/tus/code/HEAD/tree/tjacobs 我记得一个问题,光标不在 JTextArea 底部,箭头键返回控制台文本的历史记录。但希望它能让你开始
-
谢谢。我尝试了 TextAreaReader 类并还集成了输出流。
标签: java swing applet awt jsch