【问题标题】:JSCH Channel Shell, Input / OutputJSCH 通道外壳,输入/输出
【发布时间】:2013-10-21 18:14:04
【问题描述】:

我在使用 JSCH 和向 shell 发送命令时遇到了一些问题。

我有一个控制台 GUI 窗口设置,并且 system.out 已被重定向到 TextArea,这工作正常,但是我无法输入任何命令

这是会话的连接代码

    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) {
    }

但是点击“发送”按钮什么也没做。我显然错过了一些简单的东西

【问题讨论】:

    标签: java shell netbeans jsch


    【解决方案1】:

    我发现我需要将 PrintStream 声明为 Private 所以

     private PrintStream print;
    

    然后在我将初始 PrintStream 创建为

     print = new PrintStream(pop); 
    

    我能够在程序的其他部分访问它而不是创建新的部分,所以最后我需要在我的发送命令中是

     String send = jServerInput.getText();
     print.println(send);
    

    【讨论】: