• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

copying the dos window output to a file

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Java application that I've been working on and I wanted to create a function that piped all the console output to a txt file so it could be viewed by the user and used for debugging. Do you have suggestions on how to implement this?
 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you are after the compiler errors that might come out during compilationand you want to save them to a file then this would work.
 
Chris Masters
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there away to catch the entire output?
 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
like ? if you have some input from a dos or just printing something to it just open a stream and write everything to it .
 
Chris Masters
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just want to automatically capture the output stream that is being displayed in the dos window. I want to keep it as an execution log for debugging purposes.
 
Chris Masters
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried you code and it is working very well thank you. One follow up question is there a good way too implement thes recursive to capture the output continously throughout runtime? There must be perhaps I am supposed to make the class recursive check the output stream throughout runtime. Thank you very much for your help,
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's probably easiest to do this outside java, using system coimmands to redirect the output of the java process. On Unix Bourne/Korn shells or NT for example, use something like:
java myClass >outputFileName.txt 2>&1
The first > redirects standard output to the named file; the "2>&1" basically says to take the error output (signified by 2) and redirect it to the same file standard output (signified by 1) is going. This way everything goes to the same place. If you prefer you can direct standard and error output to different places. Consult documentation for whatever system you are using - it's certainly worthwhile to know how to do this easily.
It's also possible to do this from within Java:
<code><pre>
PrintStream out = new PrintStream(new FileOutputStream("outfile.txt));
System.setOut(out);
System.setErr(out);
</pre></code>
Same effect, and platform-neutral. You must recompile to reedirect to a different file - but you could write a main() method to accept a command-line argument for the file to redirect to (if any), for maximum flexibility. Enjoy.
 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic