25 Aug 2012

Sending java compiler messages to a file


javac is standard tool to compile the java source files. Successful compilation will result in bytecode whereas the unsuccessful compilation will result in the error message(s). By default all  error messages goes to System.err (which is console window).


javac has number of standard and non-standard optins which you can supply at the time of compiling a file.The standard options are those options which a supported in current jdk and will also be supported in future releases.The non-standard options are those options which are supported in current jdk but subject to change in future releases.

One of non-standard option is -Xstdout filename ,this option sends the compiler messages to the specified file rather than sending to System.err (which is console window). filename is a name of file with extension. It  could be any text file (poi.txt). Interesting thing is one can even specify filename as a html file (poi.html). You can experiment with other files extensions.
The specified file may or may not be present. If file is not present then specified file will be created in current directory and compiler messages would be sent to that file.

Lets walk with code :
In following class Stdout.java  there is two errors in line 6 and 8. Error in line 6 is becuse of local varuable "name"is not being initialized. Error in line 8 is because of a Exception object is neither handled nor declared to be thrown.


Now compile Stdout.java with -Xstdout filename option. The poi.txt is file which will contain the compiler messages. At the time of issuing following command poi.txt is not present that means it will be created.

           cmd>javac -Xstdout poi.txt Stdout.java


And here is the content of poi.txt file consisting two error messages.
             

It  is fun to find out the different things one can do with in-built java tools.
I hope you will find it fun too, please share your comment.

No comments:

Post a Comment