tags:

views:

202

answers:

4

Suppose i execute a command in java using the exec() function and i store the reference in a Process . How do i write into the input stream of that process

Process P = Runtime.getRuntime().exec("cmd /c start telnet"); 
System.out.println("done running .."); 
OutputStream output = P.getOutputStream(); 
BufferedOutputStream out = new BufferedOutputStream(output); 
String S = "open\n"; 
byte[] BS = S.getBytes(); 
out.write(BS); out.close();

I had done that but its not workin.......... above is my code attached

+1  A: 

You write to the output stream not the input stream:

Process p = Runtime.getRuntime().exec(..);
OutputStream os = p.getOutputStream();
BufferedWriter bos = new BufferedWriter(new OutputStreamWriter(os));
bos.write("whatever u want");
Suraj Chandran
Process P = Runtime.getRuntime().exec("cmd /c start telnet"); System.out.println("done running .."); OutputStream output = P.getOutputStream(); BufferedOutputStream out = new BufferedOutputStream(output); String S = "open\n"; byte[] BS = S.getBytes(); out.write(BS); out.close(); I had done that but its not workin above is my code attache
Akash
Did you try with BufferedWriter
Suraj Chandran
Maybe if starting from Windows and sending the command to UNix, the \n after open may create some problem
Suraj Chandran
+1  A: 

Seems like you actually want the Process' OutputStream, because you want to send data to the process (unless I misunderstood your question).

Here is an example.

iWerner
Its possible he wants to write to the input stream to simulate user input.
theycallmemorty
A: 

Process P = Runtime.getRuntime().exec("cmd /c start telnet"); \ System.out.println("done running .."); OutputStream output = P.getOutputStream(); BufferedOutputStream out = new BufferedOutputStream(output); String S = "open\n"; byte[] BS = S.getBytes(); out.write(BS); out.close();

I had done that but its not workin ......above is my code attached

Akash
A: 

I don't think you need the cmd /c bit in your exec call. Since exec itself will spawn a shell for you. Regardless, process handling in Java is a real pain. If you can I suggest you use the Apache exec package. It handles a lot of the low level pain for you.

Chris Gow