简体   繁体   English

如何在Java中读取/写入进程

[英]How to Read/Write to a Process in Java

I'm trying to gain access to a running process that I know the name of so that I can read and write to it. 我正在尝试访问一个我知道其名称的正在运行的进程,以便可以对其进行读写。

How can this be done in Java? 用Java如何做到这一点?

There are many ways. 有很多方法。 What you need isn't clear enough to know which one is suitable for your need. 您所需要的还不够清楚,无法知道哪一种适合您的需求。

  1. You could launch the JVM with debugging flags, allowing you to attach a debugger (gaining read and write access to the internals of your running program). 您可以使用调试标志启动JVM,从而允许您附加调试器(获得对正在运行的程序内部的读写访问权限)。
  2. You could read and write to and from the standard input, standard output and standard error streams, launching the program with those streams redirected to other programs or pipes. 您可以在标准输入,标准输出和标准错误流中进行读写操作,并以这些流重定向到其他程序或管道的方式启动程序。
  3. You could open a socket and do network communications between one program acting as a client and another acting as a server (peer-to-peer is just were all client programs act as servers too). 您可以打开一个套接字,并在一个充当客户端的程序与另一个充当服务器的程序之间进行网络通信(对等就是所有客户端程序也充当服务器)。
  4. You could communicate through a third-party item, like a message queue, a data base entry (bad idea, but we've all seen it), a file on disk (again bad idea), a shared memory buffer, a name pipe, a shared semaphore etc. 您可以通过第三方项进行通信,例如消息队列,数据库条目(不好的想法,但我们都已经看到了),磁盘上的文件(同样是不好的想法),共享内存缓冲区,名称管道,共享信号灯等。

Unfortunately the details of each possibility are too great to post in such an answer; 不幸的是,每种可能性的细节都太多了,以至于无法发布这样的答案。 and, would probably be noise if they don't match well with your communication goals. 并且,如果它们与您的沟通目标不符,可能会成为噪音。 I suggest you alternate between reading up on some of these technologies and thinking about what style and requirements you have for communication. 我建议您在阅读这些技术中的一些与思考您对通信的风格和要求之间进行选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM