4

I'd like to connect two programs and I'm stuck, Googled everything (probably I was doing something wrong) so now I'm turning to you.
So the structure is the following, the first one (let me call it ProgramA) and the second one (let me call it ProgramB) are different softwares with different tasks.

The core of the problem is, I don't exactly know how to send a message to ProgramB via command line.
Actually I can execute ProgramB via ProgramA this way of course:

Runtime.getRuntime().exec("... ProgramB ...");

Forgot to mention that ProgramB also has a System.in reader.
How to continuously send messages to ProgramB with ProgramA, that's started from ProgramA and listens for more messages via command line?

For example: ProgramA -> ProgramB (Waiting for incoming messages)
What to do now to send something to a program that runs separately and is working like a command line?

Szilárd Dóró
  • 93
  • 1
  • 10
  • You can use a quartz scheduler, providing a cron expression that would run at your designated time, for exact code details need to know what inputs you pass to program B and the expected output – Mudassar Jun 26 '15 at 20:58
  • 1
    Does the solution for passing information have to be command line based ? There are techniques like RMI which allow you to make a listening application (in your case ProgramB) and a talking application (ProgramA). RMI can be used from a command line based application ProgramA but will not use command line for passing information. – Marged Jun 26 '15 at 20:58
  • what OS do you run, what kind of program is A and B (what language/binaries) – MrSimpleMind Jun 26 '15 at 21:04
  • why Im asking for OS, is .. I'm thinking of a scenario where you need to continue using ProgramA and not blocking it as ProgramB is running, or? Maybe you will need to run ProgramB in something like `nohup` nohungup typ – MrSimpleMind Jun 26 '15 at 21:11
  • 1
    This is **NOT** a duplicate. – xrisk Jun 26 '15 at 21:13
  • 2
    This is definitely **NOT** at duplicate, and absolutely not with the suggested on. – MrSimpleMind Jun 26 '15 at 21:14
  • answer is the same to both regardless of the wording of the question –  Jun 26 '15 at 21:20
  • I'm running the software on Raspbian, but the solution should work on every OS aswell, at least I think. So you can imagine ProgramB like Terminal on Linux (reads System.in, when user presses Enter, then processes the actual command, then does his job) and ProgramA is a completely different thing, I can open ProgramB, but I don't want to use it as a standard executable program with parameters, then a program that I explained above. – Szilárd Dóró Jun 26 '15 at 21:24
  • @JarrodRoberson here the problem involves launching a process, and passing input to it. None of the linked questions refer to anything remotely like that. OP already knows about `exec` – xrisk Jun 26 '15 at 21:26
  • 1
    @Szilárd I think you will find this useful http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html – xrisk Jun 26 '15 at 21:27
  • @RishavKundu It's working in simple cases like executing a program, but the "afterwork" is the problem, that program does not exit after executing and doing job, it's waiting for more inputs via System.in – Szilárd Dóró Jun 26 '15 at 21:29
  • I know. ProcessBuilder will let you pass input to the second program. – xrisk Jun 26 '15 at 21:29
  • Imagine a simple example (-> indicates execution, => indicates connection): MyClient => [MyServer] -> [MyProgram - Doing job depending on input] that has a System.in reader (after execution it just waits and waits, until some input is entered (as I mentioned before)), now how to send more messages via [MyServer] to the already running [MyProgram]? – Szilárd Dóró Jun 26 '15 at 21:33
  • @Szilárd neuronaut’s solution is what I was suggesting. – xrisk Jun 26 '15 at 21:33
  • 1
    Alright, it seems to be working, thanks for your time and help. – Szilárd Dóró Jun 26 '15 at 21:39

2 Answers2

2

There are many ways to perform interprocess communication. One simple way that fits your scenario would to have ProgramA write to standard out (e.g. System.out.println("some message");) whenever it needs to send a message. Then, have ProgramB execute a loop that reads from standard in:

while(true) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String input = reader.readLine();
    // do something with the input
}

Just make sure you have some bit of input that can cause the loop to end so that ProgramA can cause ProgramB to shut down nicely when it needs to.

To use standard in/out you can fire up both processes from the command line and pipe the output of one to the other, something like this:

java com.example.ProgramAMainClass | java com.example.ProgramBMainClass

However, since it looks like you are launching ProgramB from within ProgramA you can get the launched process's output stream and write to it (instead of standard out). Note that it's called an output stream since ProgramA will be outputting to it even though from ProgramB's perspective it's an input stream.

Process programBProc = Runtime.getRuntime().exec("... ProgramB ...");
Writer writer = new OutputStreamWriter(programBPrice.getOutputStream ());
// and wherever you need to write a message from programA, do this:
writer.write("some message\n"); // make sure you include the newline

Reading these messages from ProgramB in this case is done exactly the same as above using standard in.

Note that you can have ProgramA read responses from ProgramB if you need that. You can accomplish this by getting the process's input stream in ProgramA and then reading from it. ProgramB can send responses using System.out.println("some response");

neuronaut
  • 2,689
  • 18
  • 24
  • how would you connect sysout to sysin? – xrisk Jun 26 '15 at 21:11
  • this is wrong, System.in and System.out are blocking –  Jun 26 '15 at 21:12
  • @JarrodRoberson of course they're blocking, but that doesn't make this a wrong solution. Why do you think that blocking IO is the wrong thing here? – neuronaut Jun 26 '15 at 21:18
  • @RishavKundu That's up to how you launch the second process. I'll update my answer to include that since it would be useful. – neuronaut Jun 26 '15 at 21:19
  • Maybe a link to http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html and/or a mention of `destroy()` ? – xrisk Jun 26 '15 at 21:41
0

If you want to transfer data between a bunch of programs you made the easiest (and the best) way to do it is to use Mediums. So you have program A and B running. how can we transfer data between them without interrupting them? what if one of the programs is written in a language too different from the first one? The answer is a medium file where you store your data .

this is the map of the whole process:

Program A >> File.log Program B << File.log

and vice versa...

YoloWex
  • 139
  • 7