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();
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.
The first step would be to read all the data that process outputs, which can be done using Process.getInputStream()
method
Process p = new ProcessBuilder("top").start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
}