Communicating with a C ++ Process with Java

Firstly, I saw several questions about this problem on the site, but did not see the answer that solves my problem.

I have a program written in Java and it calls the cmd program written in C ++. (this is an assumption since I have no actual source). I know the expected input / output of a C ++ program, in cmd these are two lines of output, and then it expects a line input. I know that the first output line of the program is through the error stream, and I get it correctly (this is expected), but I do not get the second line in the error or in the input stream. I tried to write a program immediately after the first line (error line) and did not get stuck, but there was no answer. I tried to use 3 different streams for each stream, but again, nothing was received in the input / error stream after the first line, and the program did not respond to recording through the output stream.

My initializers:

Process p = Runtime.getRuntime().exec("c:\\my_prog.exe"); BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream())); BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream())); 

Is it possible at all, or perhaps it depends on a C ++ program?

Thank you Binyamin

0
source share
3 answers

If you want to call native applications, such as C and C ++, from Java, you need to use JNI.

0
source

I would suggest putting input into the program when it starts, it will use it as input when it wants.

0
source

This is how I run any command line in Java. This command line can execute any program:

 private String executionCommandLine(final String cmd) { StringBuilder returnContent = new StringBuilder(); Process pr; try { Runtime rt = Runtime.getRuntime(); pr = rt.exec(cmd); BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream())); String line = null; while ((line = input.readLine()) != null) { returnContent.append(line); } input.close(); LOG.debug(returnContent.toString()); // return the exit code pr.waitFor(); } catch (IOException e) { LOG.error(e.getMessage()); returnContent = new StringBuilder(); } catch (InterruptedException e) { LOG.error(e.getMessage()); returnContent = new StringBuilder(); } return returnContent.toString(); } 
0
source

Source: https://habr.com/ru/post/1345017/


All Articles