- Bash Command to Check if Oracle or OpenJDK Is Installed
- 2. Introduction to the Problem
- 3. Standard Output and Standard Error
- 4. Using the grep Command with the -q Option
- 5. Conclusion
- How to Check Java Version Installed on Linux
- Method 1: Check the Java Version On Linux
- Method 2: Find Version by Checking Path Where Java is Installed
- Method 3: Search for Java in the Installed Packages List
- Verify Java Installation on Debian and Check the Installed version (if any)
- Get the Java version
- Check if Java is installed
- Search for Java in the Installed Packages list
- Using the dpkg utility to find which java package is installed
- Search
- About This Site
- Latest Tutorials
Bash Command to Check if Oracle or OpenJDK Is Installed
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:
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.
How to Check Java Version Installed on Linux
How do I check my current Java version? There are several ways to check if Java is installed and which version is running on your system.
In this tutorial, learn how to check the Java version installed on Linux distros, including Ubuntu, CentOS, and Debian.
- A user account with sudo privileges
- Access to the command-line/terminal window
- A version of Java
Method 1: Check the Java Version On Linux
To check the Java version on Linux Ubuntu/Debian/CentOS:
2. Run the following command:
3. The output should display the version of the Java package installed on your system. In the example below, OpenJDK version 11 is installed.
Note: If the output indicates there is no such package on the system, you can install it with the help of one of our guides – How to install Java on Ubuntu or How to Install Java on CentOS.
You can also check the version of the primary Java compiler – javac (pronounced “java-see”) with the command:
Method 2: Find Version by Checking Path Where Java is Installed
There are two ways to find the path of the Java directory.
The first option includes running a single command:
update-alternatives --list java
The system should respond with the path where Java is installed.
Note: This option may not work on CentOS systems. If you have issues finding the path of the Java directory with the command above, use the alternative outlined below.
Alternatively, you can use the whereis command and follow the symbolic links to find the Java path.
The output tells you that Java is located in /usr/bin/java.
2. List the content of the /usr/bin/java directory:
Inspecting the directory shows that /usr/bin/java is only a symbolic link for /etc/alternatives/java.
3. Just like in the previous step, list the content of the provided path by running:
Finally, the output displays /etc/alternatives/java is another symbolic link and that the real path of the Java directory is /usr/lib/jvm/java-11-openjdk-amd64/bin/java.
Method 3: Search for Java in the Installed Packages List
You can also prompt the system to list installed packages and search for Java, with its version number.
Find Java by listing all installed packages.
1. To generate a list of all installed packages, use the command:
2. Scroll up/down until you find the Java packages as shown in this example.
To avoid searching through all installed packages, list Java packages only. Prompt the system to list a specific software package. In this case, the package name is openjdk:
sudo apt list --installed | grep -i openjdk
Note: CentOS users need to modify the commands for listing installed packages for their package manager. Use the commands: sudo yum list installed and sudo yum list installed | grep -i openjdk instead.
With this article, you have successfully checked the Java version installed on Linux. We also covered checking the Java path and searching for Java among the installed packages.
Once the Java version is confirmed, you can start developing anything from lightweight mobile to desktop applications.
Verify Java Installation on Debian and Check the Installed version (if any)
Java is one of the most common programming languages that is used for developing and running a wide range of applications. By default, Linux distributions including Debian does not ship with the java (Java Runtime Environment, JRE) installed. However, sometimes in order to run certain programs, we may need to know if the java is running in our system or not and also what version it is currently running.
In this article, we will learn different ways using which you can verify if Java is installed in your system along with its version. We have used Debian 10 for running the commands and procedures mentioned in this article.
We will need the Terminal application in order to run the procedure. To launch Terminal in your Debian OS. go to the Activities tab in the top left corner of your desktop. Then from the search menu that appears, search for the Terminal application and launch it.
Get the Java version
Enter the following in the Terminal to check which version of java is installed.
It will verify the java runtime environment has installed on your system along with the version of java you are running on your system.
If you receive the output similar to what is shown in the following image, then Java is not installed in your system.
If you have Java installed on your system, you will see the Java OpenJDK and JRE version details as follows:
Check if Java is installed
Another way to verify java installation is to check the path where Java is installed. To do so, run the following command in Terminal:
You will receive the following output if java is installed on your system otherwise you will receive no output.
Search for Java in the Installed Packages list
You can also verify any software installation in your system by searching it from the list of installed packages. The following syntax can be used for this purpose:
$ sudo aptitude search PackageName
To verify if any Java package is installed in your system or not, run the following command in Terminal.
The above command will display all JDK packages available in Debian repositories. The first letter of each line represents the package’s current state, a line starting with “i” prefix is the one which is installed on your system.
From our below output, you can see that Java openjdk-11-jdk is installed in our system. If you don’t find the prefix “i”, that will indicate the java is not installed in your system.
Using the dpkg utility to find which java package is installed
Dpkg is the command-line utility that is used to obtain the list of installed packages on the Linux OS. We can use it to find the version of java installed in your system. to do so, run the following command in Terminal:
Using the above-described methods, you can verify if Java is installed on your system or not. Along with this, you can verify the version of java installed.
Search
About This Site
Vitux.com aims to become a Linux compendium with lots of unique and up to date tutorials.