Java - Read output stream of existing process

我只是一个虾纸丫 提交于 2020-01-03 03:07:07

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!