问题
I need to read the output of a process running on IBM j9 (emulator JVM to windows mobile). I tried this:
Process p = Runtime.getRuntime().exec("j9.exe");
BufferedReader br = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String stringLog;
while ((stringLog = br.readLine()) != null) {
System.out.println(stringLog + "\n");
}
But it didn't work because it returned a new instance of j9.exe
, not the existing process.
I need to get all messages that are being logged to System.out from j9console (of the existing process). How would I go about doing that?
回答1:
It is not possible using pure Java. You would have to write a native library that makes low level OS function calls, and expose the library to Java via JNI. Even at the native level, the OS may not have system calls that provide this capability, depending on the OS -- it is an unusual task you are asking for, something that is usually done only by debuggers. E.g. gdb.
回答2:
You would have to arrange for the process to write its output to a named pipe, and have Java read that pipe. Hope you're not on Windows!
来源:https://stackoverflow.com/questions/9539863/java-read-output-stream-of-existing-process