Ivan
Ivan

Reputation: 64207

How can 2 local Java applications interact?

Having 2 bare console Java applications running on the same machine at the same time I want to have, for example, something like a friend.println(String s) im my first app causing the second app to output s to its stdout (while there is to be silence in the first app stdout).

How to implement this?

Upvotes: 1

Views: 2105

Answers (5)

jefflunt
jefflunt

Reputation: 33954

You have to pass a message between the two somehow. Since they won't both be running inside the same JVM, you have to use a communication medium that can talk between separate processes on a machine.

You could:

  • Use RabbitMQ (tutorials can be found here)
  • Use RMI
  • Pipe the output from one to the other
  • Shared file access: have app2 monitor a file that app1 writes to, and react to any new entries in that file
  • Use network sockets: Have app2 make a network socket available to receive communication from app1 (via connecting to localhost)

Which one you choose really depends on what you want to accomplish, and how complex you need it to be. There are pros/cons to each approach. Some of the concurrent/file-based options might produce deadlocks, for example, but any one of those choices should provide a viable method for accomplishing what you describe. The learning curves on these technologies aren't too difficult if there are some you're not familiar with.

Upvotes: 2

Reverend Gonzo
Reverend Gonzo

Reputation: 40811

The easiest is probably Java RMI.

The most powerful would be a custom protocol via TCP/IP.

Upvotes: 1

Amir Raminfar
Amir Raminfar

Reputation: 34149

Here are some ways I can think of:

  • Using sockets
  • Using a file and appending to that file when a new message arrives
  • Java Message Service
  • Using a database

Probably of all using a JMS is the best.

Upvotes: 0

fernacolo
fernacolo

Reputation: 7439

Establish a connection between two using sockets, pipes, shared memory or something else (maybe JRMI can be used). Design a protocol and/or API for them to communicate, define a println service, and have one app call another.

Upvotes: 2

Brandon Buck
Brandon Buck

Reputation: 7181

Sockets or RMI. A Shared-access file that both check periodically. I would suggest either low level sockets or RMI though.

EDIT

Adding some links.

From BalusC - Oracle Sockets Tutorial

And RMI

Upvotes: 0

Related Questions