• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

writing process output stream (p.getOutputStream) to a file

 
Rashid Darvesh
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
i am calling a java process from my web UI. i am using runtime.exec to execute an external java program. i need to redirect the process output stream to a file. That means all the system.out and system.err should be written to a file. i dont how to redirect the p.getOutputStream() to a file. Here is the code which i am using which does not work
Process p = Runtime.getRuntime().exec("java -cp some program); BufferedOutputStream o = (BufferedOutputStream) p.getOutputStream();
String str = o.toString();
if (str !=null){
byte buf[] = str.getBytes();
OutputStream os = new FileOutputStream("syndierr.txt");
os.write(buf);
os.close();
}
Any help will be appreciated.
Rashid
[ February 04, 2006: Message edited by: Michael Ernest ]
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
 
Rashid Darvesh
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need to know how can i direct the process output stream into a file. Do i need to convert the process output stream to an inputStream and then pass it as a source for the File OutputStream to read it into the file. its a bit confusing. i tried using buffer input stream and output stream but then also the file is having contents like. java.io.BufferedOutputStream@228a02
Any examples
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the standard error and standard output streams of your process are separate streams. if you want to merge them, I suggest using ProcessBuilder.
 
Joe Ess
Bartender
Posts: 9313
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rashid Darvesh:
i tried using buffer input stream and output stream but then also the file is having contents like. java.io.BufferedOutputStream@228a02
Any examples

You are writing the stream object to the output file. You need to read the content of the stream and write the content to the output file. Have a look at the Java Tutorial on IO for the basics of working with Java IO.
 
Mr. Ankur Garg
Greenhorn
Posts: 6
Java MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey to read the output of any process you should use
getInputStream of that process to get the output
and getOutputStream serves as an input to process
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic