Java - 读取现有进程的输出流 [英] Java - Read output stream of existing process

查看:677
本文介绍了Java - 读取现有进程的输出流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要读取在IBM j9上运行的进程的输出(模拟器JVM到windows mobile)。我试过这个:

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");
}

但它没有用,因为它返回了一个新的<$ c实例$ c> j9.exe ,而不是现有进程。

But it didn't work because it returned a new instance of j9.exe, not the existing process.

我需要从j9console获取所有正在记录到System.out的消息(现有程序)。我该怎么做?

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?

推荐答案

使用纯Java是不可能的。您必须编写一个本机库来进行低级OS函数调用,并通过JNI将库公开给Java。即使在本机级别,操作系统也可能没有提供此功能的系统调用,具体取决于操作系统 - 这是您要求的一项不寻常的任务,通常只能由调试器完成。例如。 gdb

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.

这篇关于Java - 读取现有进程的输出流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆