Output of forked child process in Java?

StreamGobbler s are the way to go google for them. They start in new threads and are reading the output.

StreamGobblers are the way to go google for them. They start in new threads and are reading the output. A nice article describing how you implement them.

When Runtime.exec() won't.

Reading the output is not at all a problem for me. What my problem is is the input. – user919860 Aug 31 at 17:36.

The Java program controls the input from process.getOutputStream(). If you want to send data to the program, you need to send it from Java.

I don't get what you're saying. Isn't that EXACTLY what I'm doing? Did you read my sample code?

– user919860 Aug 31 at 16:48 1 I read the code again and I can't see where you are writing to out. Can you point it out to me? – Peter Lawrey Aug 31 at 19:11.

Here you have a complete sample. Notice that this run the dir command thru the output stream. Import java.io.

BufferedReader; import java.io. IOException; import java.io. InputStream; import java.io.

InputStreamReader; class StreamGobbler extends Thread { InputStream is; String type; StreamGobbler(InputStream is, String type) { this. Is = is; this. Type = type; } public void run() { try { InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine())!

= null) System.out. Println(type + ">" + line); } catch (IOException ioe) { ioe.printStackTrace(); } } } public class GoodWindowsExec { public static void main(String args) { try { String osName = System. GetProperty("os.Name"); String cmd = new String1; cmd0 = "cmd.

Exe"; Runtime rt = Runtime.getRuntime(); Process proc = rt. Exec(cmd); proc.getOutputStream(). Write("dir \n".getBytes()); proc.getOutputStream().flush(); // any error message?

StreamGobbler errorGobbler = new StreamGobbler( proc.getErrorStream(), "ERROR"); // any output? StreamGobbler outputGobbler = new StreamGobbler( proc.getInputStream(), "OUTPUT"); // kick them off errorGobbler.start(); outputGobbler.start(); // any error? Int exitVal = proc.waitFor(); System.out.

Println("ExitValue: " + exitVal); } catch (Throwable t) { t.printStackTrace(); } } }.

Here, you're writing to the command prompt by using the OutputStream's write method and writing the command with the line termination character to the method as a set of bytes. I've been using the OutputStream's println method and writing strings without the line termination character and strictly as strings. Was that my problem the whole time?

That the only way that I can write to an external program is to write a series of bytes to the OutputStream's write method? – user919860 Sep 1 at 16:11.

You are confusing things. First of all, the current description is chaotic and you should rewrite it. Second, do the StreamGobbler as provided in Farmor's answer.

What do you expect from out. Println("My Garbage String"); while((line = readLine())! = null) ...? It does exactly what it says.

Sends a string to external process' input stream than waits at the same input stream for any string to arrive. The one you sent before is of course long gone at the time.

It hasn't been sent as he haven't flushed the stream. – Farmor Aug 31 at 18:36 I am not sure if you have to flush it, but it would not change anything. The code he uses does not make sense.

– MarianP Aug 31 at 19:14.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions