【问题标题】:send linux commandline output to another screen将 linux 命令行输出发送到另一个屏幕
【发布时间】:2016-12-21 18:36:58
【问题描述】:

我想在我自己的 java 应用程序中读取正在运行的应用程序的所有输出。目前我有 2 个屏幕,第一个是输出应用程序永久打印新信息,第二个是 java 应用程序。不幸的是,不可能在一个屏幕上同时运行两个应用程序。我的想法是将所有输出通过管道传输到 java 应用程序屏幕以在那里读取它,但我做错了或者它不起作用。

我的(测试)服务器如下所示:

public class Main {

public static void main(String[] args) throws InterruptedException {

    while (true) {
        Scanner scanner = new Scanner(System.in);
        String output = scanner.next();
        System.out.print("JAVA " + output);

    }
}

}

我的 linux 启动文件是这样的:

#!/bin/bash

cd ../../raspberry-remote/

screen -dmS smarthome_javaserver #here runns the java  application
screen -dmS smarthome_receive | smarthome_javaserver  #send the output to the java screen

#start java app
screen -S smarthome_javaserver -p 0 -X stuff "java -jar ServerReceiver.jar^M"

#start receive tool
screen -S smarthome_receive -p 0 -X stuff "pilight-daemon -D^M"
screen -S smarthome_receive -p 0 -X stuff "pilight-daemon -D^M"


echo "started receiver"

任何人都知道如何实现这一点,或者有没有办法让它们在一个屏幕上运行?感谢您的帮助。

【问题讨论】:

    标签: java linux debian gnu-screen


    【解决方案1】:

    对此我不太确定,但我可以为您指出我认为的正确方向。首先,您必须启动可以使用“ProcessBuilder”执行的应用程序,在创建流程后,您需要一个输入流阅读器来读取输出。

    在代码中它看起来像这样:

    ProcessBuilder b = new ProcessBuilder("myapp");
    Process app = builder.start();
    
    //app is running now read its output
    InputStreamReader stream = new InputStreamReader(app.getInputStream());
    

    您现在可以使用界面更简单的 BufferedReader 读取我推荐的应用程序的输出。我总是从这样的流中读取:

    BufferedReader br = new BufferedReader(stream))
    
    for(String line; (line = br.readLine()) != null; ) 
    {
       ProcessLine(line);
    }
    

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-18
      • 1970-01-01
      • 2011-02-22
      • 2013-11-08
      • 1970-01-01
      • 1970-01-01
      • 2012-10-25
      • 2021-09-30
      相关资源
      最近更新 更多