1

I have written a Java project that uses Runtime.getRuntime.exec() to launch a .exe which then launches a python script.

Within the py script are several 'print(..)' commands that I wish to be read by java using getInputStream(). However, as the script is being launched by another .exe, 'print()' or sys.stdout.write() outputs do not appear in the cmd window. Is there a way to ensure all outputs return to cmd, or is there another way round it?

Thanks in advance!

Prabhat G
  • 2,974
  • 1
  • 22
  • 31
Harry
  • 11
  • 2
  • Possible duplicate of [Starting a process in Java?](https://stackoverflow.com/questions/3774432/starting-a-process-in-java) – Federico klez Culloca Feb 28 '18 at 11:16
  • unfortunately not – Harry Feb 28 '18 at 11:23
  • Fortunately yes. You open a `Process` which gives you an `InputStream` from which you can get the output of the program and then you print them (or do whatever you need to do). Am I understanding your question wrong? – Federico klez Culloca Feb 28 '18 at 11:25
  • Yeah, so I've got the process running fine from Java, but the python output never reaches the cmd window, so it can't be read by Inputstream(). I'm trying to find out how to get the py script to print to cmd when its called by another .exe – Harry Feb 28 '18 at 11:29
  • It should print without a problem, you just need to capture its output and re-print it as explained in the linked answer. – Federico klez Culloca Feb 28 '18 at 11:31
  • the issue is it's not printing to cmd as it's being run by another .exe. Maybe the question should be, how do you get python to print to cmd when executed by another program? – Harry Feb 28 '18 at 11:39
  • Can you please confirm that if you open a terminal and launch the python script with the same parameters you user from the java version it actually prints? – Federico klez Culloca Feb 28 '18 at 11:42
  • if I run the script straight from cmd, it prints. if, when in cmd, I launch my desired .exe which then launches the same py script, there is no output – Harry Feb 28 '18 at 11:48
  • I'm starting to think it's the .exe's fault. How does it call the python script? Please modify your answer to include this information. If you can't modify the .exe I doubt there is much you can do. – Federico klez Culloca Feb 28 '18 at 11:52
  • ok thought so, just wondered if there was a way around it. Thanks anyway – Harry Feb 28 '18 at 12:08
  • Does your `getInputStream()` ever receive the output? If so, print it to the console there. – lit Feb 28 '18 at 13:57

0 Answers0