Search code examples
stdoutjavacstderr

How to store the result of compilation (using javac) to a text file?


I want to compile a Hello World program. I use javac Hello_World.java. However in cmd terminal window, it shows "semicolon is missing" compilation error.

How do I store this compilation error into a text file? Or even a string will do. How do I "catch" this error? I tried

javac filename.java 2 > textfile.txt

But javac still prints stderr and stdout to screen.


Solution

  • Get rid of the space between 2 and >

    javac filename.java 2> textfile.txt
    

    Another way is by redirecting stderr into stdout:

    javac filename.java > textfile.txt 2>&1
    

    More info: https://support.microsoft.com/en-us/help/110930/redirecting-error-messages-from-command-prompt-stderr-stdout