开发者

How to write into input stream in java

开发者 https://www.devze.com 2022-12-10 01:11 出处:网络
Suppose i execute a command in java using the ex开发者_StackOverflowec() function and i store the reference in a Process . How do i write into the input stream of that process

Suppose i execute a command in java using the ex开发者_StackOverflowec() 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


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消