Thursday, January 19, 2012

Capture Console Output


To capture the output from console, we need to add "2> filename" for errors and "> filename" for output along with command.

Sample Program: 


class Check{
      public static void main(String[] args){
            System.out.println("Leninkumar Koppoju");
            System.out.println(1/0);
      }
}


Compilation
Running
Generally
javac Check.java
java Check
Now
javac Check.java 2> compile.txt
java Check > output.txt 2> errors.txt

To generate compilation errors, I have removed the semicolon while doing compiling.

compile.txt:

Check.java:3: ';' expected
                        System.out.println("Leninkumar Koppoju")
                                                                ^
1 error

output.txt:

Leninkumar Koppoju


errors.txt :

Exception in thread "main" java.lang.ArithmeticException: / by zero
            at Check.main(Check.java:4)