Skip to content
Advertisement

Printing the compilation errors to a file in disk

I am a student of computer science. I am learning java on linux platform ubuntu. While I am compiling a program always I get some compilation error in terminal. I tried to read the error message from terminal. I think reading these error message gives me better understanding about the program/java language itself. So when I recover the error sometimes I would try to read the earlier error message, but the message sometimes removed from the terminal.

So my question is – is there any way to redirecting the error message from terminal to directly to a file.

I am using the fllowing command in ubuntu to compile –

$javac MyClass.java > file_name  

But it prints all the line from the terminal to the ‘file_name’. Is there any way to redirect only the error message?

Thanks.

Advertisement

Answer

You need

$javac MyClass.java 2> file_name 

which will redirect stderr. You can (instead) redirect stderr to to stdout and then to a file thus:

$javac MyClass.java 2>&1 > file_name 

The above assumes you’re using a Bourne-shell type shell (sh/bash/ksh/zsh), as opposed to a C-shell variant.

Having said all that, I would (if at all practical) avoid using the command line for general development in favour of an IDE, which (amongst many other advantages) will tell you a lot more info re. your compilation issues.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement