send linux commandline output to another screen

93 Views Asked by At

I want to read all output from a running application in my own java application. Currently I have 2 screens, in the first the output application prints permanently new informations and the second with the java application. Unfortunately it is not possible to run both applications in one screen. My idea was to pipe all output to the java application screen to read it in there, but I do it wrong or it doesn't work.

My (test) server looks like this:

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);

    }
}

}

and my linux start file like this:

#!/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"

Anyone knows how to realize this, or is there a way to let them run in one screen? Thanks for help.

1

There are 1 best solutions below

0
On

I'm not quite sure about this but I can point you in the right direction I think. First you would have to start the application which you could do using "ProcessBuilder" and after creating the process you would need a input stream reader to read the output.

In code it would look something like this:

ProcessBuilder b = new ProcessBuilder("myapp");
Process app = builder.start();

//app is running now read its output
InputStreamReader stream = new InputStreamReader(app.getInputStream());

You can now read in the output of app I would recommand using a BufferedReader which has an easier interface. I always read from a stream like this:

BufferedReader br = new BufferedReader(stream))

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

I hope this helps you out a little.