4

I have 2 questions

1) how do I i invoke a unix shell from a java.runtime library to run a command like this

Process p = Runtime.getRuntime().exec(commands);

cat alias > bias

2) How can I read and write to a unix pipe a steady stream of data from java. Do i have to make all the system calls like open read write to a pipe

I basically want to replicate this command

cat alias > bias

where the steady stream of data will be come from java program to bias and not cat alias.

1

3 Answers 3

6

Since JDK7, there is a convinience way to do this:

ProcessBuilder pb = new ProcessBuilder("cat", "alias");
File bias = new File("bias");
pb.redirectOutput(Redirect.appendTo(bias));
pb.start();

Reference: ProcessBuilder#redirectOutput

Sign up to request clarification or add additional context in comments.

Comments

3

You can invoke a Unix shell like you would invoke any other program. Use the -c option to pass the command to run as a parameter. Also make sure to use the exec(String[]) method, and not exec(String), to avoid the command being tokenized the wrong way:

String[] cmd = {"/bin/sh", "-c", "/bin/cat alias > bias"};
Process p = Runtime.getRuntime().exec(cmd);

To read and write from/to the process, get the input, output, and error streams from the Process instance you just created:

InputStream in = p.getInputStream();
InputStream err = p.getErrorStream();
OutputStream out = p.getOutputStream();

Then read from/write to these streams as usual.

Note that streams are named relative to the Java application. Anything written to the OutputStream is piped to the standard input of the process you created. Anything written by the process to stdout/stderr is piped to the InputStreams obtained from p.getInputStream() and p.getErrorStream() respectively.

Reference: http://download.oracle.com/javase/6/docs/api/java/lang/Process.html

Comments

1

You can't use ">" directly in a Runtime.exec() command. It won't have the expected effect, and will not redirect the output to the file.

You have to get the input stream of your process and redirect it to the file to write:

Process p = Runtime.getRuntime().exec(new String[] {"cat", "alias"});
InputStream in = p.getInputStream();
FileOutputStream fos = new FileOutputStream("yourFile");
// Write in into fos...
// handle the process streams

I don't detail the writing method but:

  • you have to handle the exceptions (IO)
  • you have to consume (at least to close) all the process streams (error, input, output).

6 Comments

"You can't use ">" in a Runtime.exec() command": Yes, you can. Why do you think you cannot do it?
@Grodriguez: I believe he wanted to explain that if you put ">" into a Runtime.exec() command, it will not have the effect it would have in a Unix shell. Of course you can use ">" as a regular character, or invoke /bin/sh as in your answer and pass ">" to it.
If that's what he meant he should have said so. Let's not second-guess shall we?
@Benoit: It will have the same effect as it would in a Unix shell, as long as you are actually executing a Unix shell in Runtime.exec().
Yes, but in my example code, I do NOT execute a Unix shell, but only the "cat" command. So, for redirecting the output, I need to use the process streams. I prefer using java stuff rather than system stuff when I can.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.