Reputation: 64207
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
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:
app2
monitor a file that app1
writes to, and react to any new entries in that fileapp2
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
Reputation: 40811
The easiest is probably Java RMI.
The most powerful would be a custom protocol via TCP/IP.
Upvotes: 1
Reputation: 34149
Here are some ways I can think of:
Probably of all using a JMS is the best.
Upvotes: 0
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
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