How to Get Main output into Common() method in Java?

What are the ways to get the output and error stream of a shell script executed from Java?

  • I need to get the output and stream of a shell script executed from Java. If I use Runtime.getRuntime.exec(<cmd>) which will return the Process object from which I can get the output and error streams. But due to memory issue I cant proceed with Runtime.getRuntime.exec(<cmd>). Is there any other way to achieve this? I am using posix_spawn to execute the child process

  • Answer:

    If you are running on an OS like Linux that has named pipes or FIFOs, you could try creating two pipes, opening them for reading in Java, then spawning your process with file descriptors for those pipes opened for writing.  I haven't done this before, and it's kludgey, but that's what I would try if faced with your circumstance. Things to consider: - Getting the order of operations right is essential.  A pipe should be opened for reading first before another process opens for writing.  IIRC. - If Java doesn't seem to be reading anything, your process might not be flushing output when you expect.  If your Java program needs the stream data real-time, flush early and often. - If you're not fussy about knowing if something came from stdout or stderr, you could use a single pipe and dup the file descriptor for both process outputs. - Google and StackOverflow are your friends.  I'm just an idea guy. Good luck!

Kris Larson at Quora Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.