-1

Ubuntu 19.10 Using inellij latest version Java

Purpose: With java code use the terminal to monitor and verify tests on android device, For example tcpdump command on android device Logcat command and so on,

The problem is that if the output always listen / write the java can't get the strings, Only if I am shutting the terminal (ctrl c) and then the java gets the strings at once,

What I need is to get the real time output that the terminal provide, seems that the terminal is locked and I understand that I need to open a socket connection to read the output in real time ?

Any help will be appreciated

Eyal
  • 17
  • 5
  • If a program buffers its output, there is nothing you can do to flush that text, unless the program has command-line options to enable auto-flush. Many programs will auto-flush when stdout is a console, but will buffer if stdout is file or a pipe, for performance reasons. – Andreas Feb 09 '20 at 09:26
  • This is a buffer reader, no files in this case – Eyal Feb 09 '20 at 09:29
  • 1
    You cannot read content of a terminal window, but you can read the output of a console program. This is described in the article https://stackoverflow.com/questions/5711084/java-runtime-getruntime-getting-output-from-executing-a-command-line-program – Stefan Feb 09 '20 at 09:41
  • @Eyal What is a buffered reader? I'm talking about the stdout as `tcpdump` sees it. Since `tcpdump` is not a Java program, there is no buffered reader. `tcpdump` will see that its stdout is a pipe. (The pipe is to your Java program, but `tcpdump` doesn't know that.) – Andreas Feb 09 '20 at 09:44
  • tcpdump is just example for a command that are continue to monitor, never quit, logcat is also a good example that I can't get the strings in real time from the buffer reader – Eyal Feb 09 '20 at 10:03

1 Answers1

0

For tcpdump, use the --immediate-mode command-line option.

--immediate-mode

Capture in "immediate mode". In this mode, packets are delivered to tcpdump as soon as they arrive, rather than being buffered for efficiency. This is the default when printing packets rather than saving packets to a ``savefile'' if the packets are being printed to a terminal rather than to a file or pipe.

Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • I guess I was missunderstood, My java code sometimes ude tcpdump, sometimes logcat on order to catch issues in real time, I am using the java to open the termial: ProcessBuilder processBuilder = new ProcessBuilder(); processBuilder.command("bash", "-c", "adb -s 192.168.1.199 shell tcpdump -vv -i eth0 -s 0 | grep error"); Process proc = null; proc = processBuilder.start(); – Eyal Feb 09 '20 at 10:00
  • The problem that in this way I can't get strings, that's really bad for me – Eyal Feb 09 '20 at 10:03
  • @Eyal See [comment by Stefan](https://stackoverflow.com/questions/60135084/how-can-i-read-from-terminal-ubuntu-output-in-java-code-output-never-ends-q/60135295?noredirect=1#comment106358704_60135084). – Andreas Feb 09 '20 at 18:41
  • @Eyal You said *"Only if I am shutting the terminal (ctrl c) and then the java gets the strings at once"*. How is Java getting the strings from the terminal, if it is not getting piped the output, as shown in the link provided by Stefan? – Andreas Feb 09 '20 at 18:44