1

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?

inexxt
  • 81
  • 1
  • 3

2 Answers2

5

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
Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
  • `2>&1` does not "redirect to stdout instead of stderr". What is does is redirect stderr to stdout. That's not the same thing ;) – fge Jul 19 '15 at 20:03
  • @Vivin Paliath - you're absolutely correct: `2>&1` redirects both stdout and stderr "to the same place". It's an excellent solution to this problem. ALSO: I didn't know until very recently that this syntax *ALSO* works in a Windows/DOS .bat file. – paulsm4 Jul 19 '15 at 20:15
  • @fge Oops you're absolutely right. That's what I meant to say :) – Vivin Paliath Jul 19 '15 at 20:45
  • 1
    Thanks. And if one wants to redirect it to pipe, to use it in grep/sed/less/anything, there is a syntax of command 2>&1 >/dev/null | grep 'sth' (from https://stackoverflow.com/questions/2342826/how-to-pipe-stderr-and-not-stdout) – inexxt Jul 20 '15 at 01:08
0

you can simply check your java versionby using this tool: http://www.java.com/en/download/installed.jsp

Muhammad Faizan
  • 863
  • 1
  • 7
  • 16