Akash
Akash

Reputation: 31

How to write into input stream in java

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

Upvotes: 1

Views: 3117

Answers (3)

Chris Gow
Chris Gow

Reputation: 7924

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.

Upvotes: 0

Suraj Chandran
Suraj Chandran

Reputation: 24791

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");

Upvotes: 2

Wernsey
Wernsey

Reputation: 5491

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.

Upvotes: 2

Related Questions