0

Possible Duplicate:
Redirect stdin and stdout in Java

I know how to execute shell commands in Java but how to do it so my application can write to its input and read from its output.

Community
  • 1
  • 1
  • 1
    Guys, it is not duplicate. It is other question. He asks not about redirect but how to implement process-to-process communication between parent and child process. – AlexR May 20 '12 at 17:35

1 Answers1

3

I think that you even explained the answer into your question.

When you are running external application from java either using Runtime.exec() or ProcessBuilder you can access standard output and standard input streams. User input stream to read what external application writes and output stream to send commands to external application.

But be careful. Some applications will not get your commands sent from java. For example command ssh in Unix system is designed to avoid its usage by non-human user (e.g. other application). It require to be executed from terminal for security reasons, so you cannot for example run ssh otherhost and then send user/password from java. If you need this you have to run the command via other command line utility named expect that simulates terminal and is driven by script.

AlexR
  • 114,158
  • 16
  • 130
  • 208