Search code examples
javahttp-redirectpipeoutputjavac

Redirecting java and javac output


I'm trying to write an installation (bash) script, in which I need to check if user has the java 1.8 installed.

The obvious way to do that is to call

javac -version | grep 1.8

, but for some strange reason javac (and java) -version output can't be redirected - neither by |, > or >> - in the first case, the second program doesn't get any input, in second and third, the output file is empty after executing the command. I've tried to check it on three different machines, the result was the same on each of them.

What is the cause of that? Is there any other way I can check the java version?


Solution

  • It appears that the output is sent to STDERR. Try this:

    javac -version 2>&1
    

    This will redirect the output of STDERR to STDOUT. Now you should be able to pipe the command.

    If you just want to redirect it to a file, just replace &1 by the filename, so:

    javac -version 2>out