1. Overview
OpenJDK and Oracle JDK are two widely used Java environments in the industry. Both of them are performant and stable. However, they have some differences.
Therefore, sometimes, we may want to quickly get the answer to the question: “Which JDK is running on this machine?”
In this tutorial, we’ll address how to check if the current Java environment is Oracle JDK or OpenJDK.
2. Introduction to the Problem
As we’ve known, the command “java -version” will print the detailed Java version information. So, for example, if we run it on a machine with Oracle JDK installed:
$ java -version
java version "15.0.2" 2021-01-19
Java(TM) SE Runtime Environment (build 15.0.2+7-27)
Java HotSpot(TM) 64-Bit Server VM (build 15.0.2+7-27, mixed mode, sharing)
The OpenJDK supports the same command, too:
$ java -version
openjdk version "17.0.3" 2022-04-19
OpenJDK Runtime Environment (build 17.0.3+3)
OpenJDK 64-Bit Server VM (build 17.0.3+3, mixed mode)
Of course, there are other JDK implementations. We can extend the solution to adapt to other JDK vendors. However, for simplicity, we’ll only check if the current JDK is OpenJDK in this tutorial.
Next, let’s see how to get the JDK provider information quickly in the Linux command line.
3. Standard Output and Standard Error
As soon as we see the two outputs of “java -version“, an idea may come up already: grep “OpenJDK” in the output of java -version.
The idea is straightforward. Let’s do a quick test with the OpenJDK:
$ java -version | grep -c 'OpenJDK'
openjdk version "17.0.3" 2022-04-19
OpenJDK Runtime Environment (build 17.0.3+3)
OpenJDK 64-Bit Server VM (build 17.0.3+3, mixed mode)
0
As we can see in the command above, we’ve passed the option -c (printing the count of matching lines only) to the grep command. However, the output contains the original “java -version” output.
Further, the “0” at the end of the output means we don’t find any line matching the pattern “OpenJDK”. This is obviously not true. Why is that happening?
This is because the command java -version writes the version information to the standard error (stderr) instead of the standard output (stdout). However, the following pipe (|) reads only the stdout of the java command and then feeds the grep command as the stdin.
Therefore, to make our idea work, we need to redirect the java command’s stderr to stdout:
$ java -version 2>&1 | grep -c 'OpenJDK'
2
This time, as the output above shows, we’ve got the expected result: 2.
Then, we can certainly write if statements to check if the output is greater than 0 to decide if the current JDK is OpenJDK.
However, let’s see a simpler approach to doing this check.
4. Using the grep Command with the -q Option
When we pass the -q option to the grep command, grep will output nothing. But, if the given pattern is not matched, the grep command exits with code 1. Otherwise, it returns with the exit code 0:
$ echo "Kotlin is amazing!" | grep -q 'Java'
$ echo $?
1
$ echo "Java is amazing!" | grep -q 'Java'
$ echo $?
0
We can make use of the exit code and conditionally concatenate multiple commands using the && and the || operators.
Let’s understand the operators through a quick example:
CMD1 && CMD2 || CMD3
In the example above, CMD1 gets executed first. Only if it succeeds (exit code = 0 ) will CMD2 start. Otherwise, CMD3 will be executed.
Therefore, we can build a command to check whether the JDK provider is OpenJDK or not:
java -version 2>&1 | grep -q "OpenJDK" && echo "It is OpenJDK." || echo "It is NOT OpenJDK."
Now, let’s test our solution on different Java environments and see if it works as expected. First, let’s run it against an OpenJDK installation:
$ java -version
openjdk version "17.0.3" 2022-04-19
OpenJDK Runtime Environment (build 17.0.3+3)
OpenJDK 64-Bit Server VM (build 17.0.3+3, mixed mode)
$ java -version 2>&1 | grep -q "OpenJDK" && echo "It is OpenJDK." || echo "It is NOT OpenJDK."
It is OpenJDK.
As we can see, it detects OpenJDK correctly. Next, let’s test it with the Oracle JDK:
$ java -version
java version "15.0.2" 2021-01-19
Java(TM) SE Runtime Environment (build 15.0.2+7-27)
Java HotSpot(TM) 64-Bit Server VM (build 15.0.2+7-27, mixed mode, sharing)
$ java -version 2>&1 | grep -q "OpenJDK" && echo "It is OpenJDK." || echo "It is not OpenJDK."
It is not OpenJDK.
The output shows the command works for this case, too.
If we use this command often, typing the long command isn’t convenient. We can create an alias in the .bashrc file:
alias chkJDK='java -version 2>&1 | grep -q "OpenJDK" && echo "It is OpenJDK." || echo "It is not OpenJDK."'
In this way, when we want to check the provider of the current JDK, we can simply launch the chkJDK alias.
5. Conclusion
In this article, we’ve learned how to check if the current JDK is OpenJDK or Oracle JDK through examples.
We’ve addressed that combining grep -q and the && and || operators sometimes can solve this kind of problem simply.