Microsoft KB Archive/252021

= How To Redirect Output from Java Code =

Article ID: 252021

Article Last Modified on 6/14/2006

-

APPLIES TO


 * Microsoft Java Virtual Machine

-



This article was previously published under Q252021



SUMMARY
This article describes how to redirect output from Java code. This procedure differs whether you are trying to redirect output for an applet or an application.

NOTE: The ability to alter the value of System.out and other system streams may be removed or changed in the future.



How to Redirect Output for an Applet
You can enable Java logging for applets in Microsoft Internet Explorer. All output that is sent to System.out or System.err is written to a file named Javalog.txt, which is found in the C:\ \Java folder (where   represents your operating system).

To enable Java logging in Internet Explorer, follow these steps:
 * 1) On the Start menu, point to Settings, click Control Panel, and then click Internet Options.
 * 2) On the Advanced tab, in the Microsoft VM section, make sure that the Java logging enabled check box is selected.

NOTE: The Internet Explorer Java logging option does not affect Java applications.

How to Redirect Output for an Application
To redirect output for Java applications, perform one of the following steps:  Use the redirection operator (>) on the command line to redirect all standard output to a file. For example, to redirect all text output to a file named Myjavalog.txt, type the following command at a command prompt:

jview MyJavaApp > myjavalog.txt

  Reassign System.out to programmatically direct the output to a file or pipe. The following code, which works for jview or wjview and Internet Explorer, demonstrates how to do this: import java.io.*; public class JavaLogger {  public static void main (String[] args) {     if("true".equalsIgnoreCase(System.getProperty("com.ms.applet.enable.logging"))) {        try {           // Code that writes to System.out or System.err String logdir = System.getProperty("java.home"); PrintStream ps = new PrintStream(                            new BufferedOutputStream(new FileOutputStream(                             new File(logdir,"javalog.txt"))), true); System.setOut(ps); System.setErr(ps); }         catch (Exception e)         { System.out.println(e.getMessage); }     }   } }                    

