1

I am writing a Java program which accesses a compiled C++ program via a ProcessBuilder. The C++ program takes a while to "start up", but once it has done so, it can take in strings of text and get an output (currently being written to a text file) very quickly.

Essentially, is there a way to have a running process "wait" for an input to be given to it, rather than have to enter the input source as it is started? I don't want to have to restart a process every time a user inputs a String, as that will take too long and is unnecessary. At the same time, I want to start the process, have it "ready", and then prompt the user for an input. As soon as the user does so, I want to send that input to the running process, collect the output, and present that. Is this possible to do?

Thanks!

Sam
  • 45
  • 5
  • What does c++ program do if it does not get the "strings of text"? – Steephen Oct 26 '15 at 19:30
  • It just waits for an input until it receives something. After it receives an input, it outputs another string based off the input, which I am capturing. Following this, it goes back to waiting for another input. Receiving inputs and producing outputs takes very little time, but starting the process takes a while. That's why I want to be able to start the process once and have it "waiting", then ping it with multiple inputs as the user inputs them, without having to re-initialize the C++ program. – Sam Oct 26 '15 at 19:34
  • So do you prefer to sleep the process of querying input string by the cpp process? – Steephen Oct 26 '15 at 19:40
  • I want to pause it to wait for input, but I do not know of any way to use ProcessBuilder (or anything similar) where an input can be supplied *after* the process has been started. – Sam Oct 26 '15 at 20:13

1 Answers1

0

If you want the Java program to "wait" for the C++, you need some way for the C++ program to tell the Java program that it is ready. You could do this by sending a message from the C++ program's output stream to the Java program, and have the Java program wait until it reads something on it's own input stream before it prompts the user for input.

On the other hand, the only reason for your Java program to wait is cosmetic. If all your communication is happening over streams, the Java program can start sending strings to the C++ at any time, and the C++ will read those strings from its input stream once it finishes starting up. Not having the Java program wait means the time between user input and received output might be longer, but the total time between starting up the C++ program and receiving the output may actually be reduced.

Nathaniel Jones
  • 1,829
  • 3
  • 29
  • 27
  • Thanks for the reply! I was wondering if there was some way to send multiple strings to the C++ program? The wait is not cosmetic; the Java program has to wait for the user to input a string. Essentially, I was wondering if it was possible to create a ProcessBuilder or something similar where the input could be given *after* starting the process, rather than before. – Sam Oct 26 '15 at 22:11
  • After you create a `Process` with your `ProcessBuilder`, you can use the input and output streams of that `Process` to send as many strings back and forth between the two programs as you want. There is no limit to the number of strings or the wait between strings so long as both programs continue to run. – Nathaniel Jones Oct 26 '15 at 23:32
  • That makes sense! Thanks a lot! – Sam Oct 30 '15 at 21:38