java - JSCH channel shell ,输入/输出

标签 java shell netbeans jsch

我在使用 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/

相关文章:

java - 找不到符号: symbol: class SimpleDate location: class SimpleDateClient

java - JApplet在HTML页面中运行失败

Java连续方法调用

unix - 使用通配符在 vi 中编辑多个文件

shell - 如何使用 Ant exec 任务运行管道命令?

java - 使用 addActionListener() 的 ActionPerformed 和 Netbeans 生成的 ActionPerformed 之间有什么区别?

java - 使用 SSH 将文件从 Android 应用程序发送到树莓派

java - 有人可以向我解释一下使用stream();的这段代码的构造吗?

java - HttpsURL 连接 : Connection Timed out error

shell脚本中的Mysql数组(Linux)