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?

3
  • You know what stdout and stderr is? (also you might want to consider writing a small Java 1.3 program which tests the facilities provided by the JVM directly using reflection) Commented Jul 19, 2015 at 19:57
  • Also, what will you do when java 1.9 comes out? Commented Jul 19, 2015 at 19:59
  • I don't really care about the 1.9 version, since the software won't be in use then. And 'small Java 1.3' program won't work if there's no java at all. Commented Jul 20, 2015 at 1:23

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

4 Comments

2>&1 does not "redirect to stdout instead of stderr". What is does is redirect stderr to stdout. That's not the same thing ;)
@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.
@fge Oops you're absolutely right. That's what I meant to say :)
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 stackoverflow.com/questions/2342826/…)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.