我在使用 JSCH 并向 shell 发送命令时遇到一些问题。
我有一个控制台 GUI 窗口设置,system.out 已重定向到 TextArea,这工作正常,但我无法输入任何命令
这是 session 的连接代码
this.channel=session.openChannel("shell");
PipedInputStream pip = new PipedInputStream(40);
this.channel.setInputStream(pip);
PipedOutputStream pop = new PipedOutputStream(pip);
PrintStream print = new PrintStream(pop);
this.channel.setOutputStream(System.out);
print.println("ls");
this.channel.connect(3*1000);
这工作正常,并运行 ls 命令并显示输出,但是如果我现在想运行更多命令,这些命令不起作用。
我有一个文本框设置和一个“发送”按钮,用于将这些命令发送到下面编码的服务器。
String send = jServerInput.getText();
try {
PipedInputStream pip = new PipedInputStream(40);
//this.channel.setInputStream(pip);
PipedOutputStream pop = new PipedOutputStream(pip);
PrintStream print = new PrintStream(pop);
//this.channel.setOutputStream(System.out);
//System.out.println(send);
print.println(send);
} catch (IOException iOException) {
}
但是点击“发送”按钮没有任何作用。我显然错过了一些简单的事情
最佳答案
我发现我需要将 PrintStream 声明为 Private 所以
private PrintStream print;
然后在我将初始 PrintStream 创建为
print = new PrintStream(pop);
我能够在程序的其他部分访问它,而不是创建新的部分,所以我最终在发送命令中需要的是
String send = jServerInput.getText();
print.println(send);
关于java - JSCH channel shell ,输入/输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19490030/