Search code examples
javalinuxcursor

Parse cursor output java


How can i parse the output from a linux cursor program like e.g top in java? I would like someone to give an example or link one. Right now i got top running like a Process object. And btw top is just an example of such a program.

String[] args={"top"};
Process process = new ProcessBuilder(args).start();

Solution

  • You can't. A program written using curses isn't outputting a stream of characters like a typical command-line program, or even one using the backspace trick. Instead, it's using operating-system specific calls like ioctl and implementation-specific escape sequences like those described in TermInfo. Generally, such programs on Unix systems are simple frontends to libraries that perform all of the necessary work, and your best option is usually to use those libraries (writing a JNI wrapper if one isn't available).

    In the case of top, you can see how the program reads the process information from the OS in its source code, available as part of the procps package.