Q: How can two Java processes (two JVMs) on the same computer interact -- that is, read each other's methods and exchange objects? I'm using RMI, but it seems that a simpler solution should exist.
A: Interprocess communication is an important programming topic, and Java, like any serious programming environment, addresses the issue. One approach, as you have already learned, is RMI. A closely related alternative is CORBA. CORBA allows you to exchange objects and dynamically invoke methods at runtime. (For a quick CORBA tutorial, see the Resources section below.)
However, like RMI, CORBA can be overkill under some circumstances. For simple interprocess communication, you can use plain old sockets to communicate between Java applications. Objects can be serialized and transmitted over sockets through the use of the
ObjectOutputStream classes. While sockets are simpler than RMI or CORBA, nothing is defined for you, so you'll have to define everything. This means that you will need to define your own communication protocols, write your own lookup and connection services, take care of security, and so on. (For a good introduction to Java socket programming, see Resources.)
I'm almost afraid to mention it, but you could always employ lock files for communication. Lock files are a primitive method of communication between processes on the same system. Conceptually, lock files are simple: to communicate, two or more processes read from and write to a well-known file on the filesystem. Because this is such a primitive approach, it is often frowned upon and not considered a legitimate form of interprocess communication.
Learn more about this topic
- "Introduction to CORBA," from the Java Developer Connection
- "Java Programming Language Basics, Part 2Lesson 1Socket Communications," also from the Java Developer Connection, provides a good tutorial on socket programming