How to Check if Java Is Installed
-
Use
java -version
Command in Windows to Check if Java Is Installed -
Use
where java
to Search for Java in Windows -
Use
which java
to Check for Java Installation in Mac -
Use
System.getProperty("java.version")
to Check Java Version in Java
This tutorial teaches us to check if Java is installed on the machine. Some software and applications require Java, and to check if our device supports it, we need to follow the below steps.
Use java -version
Command in Windows to Check if Java Is Installed
The quickest way to check if there is any installation of Java available in Windows is to use the java
command with the -version
option in a command line.
We get details like the Java Development Kit (JDK) version, the runtime environment, and the server machine in the output when we run the command.
C:\WINDOWS\system32>java -version
Output:
openjdk version "15.0.1" 2020-10-20
OpenJDK Runtime Environment (build 15.0.1+9-18)
OpenJDK 64-Bit Server VM (build 15.0.1+9-18, mixed mode, sharing)
Use where java
to Search for Java in Windows
Another way to find out if Java is installed on our Windows device is to use the where
command that searches out the specified name of the executable and returns its location.
To check for Java, we use where java
in the command line, and if Java is installed on our device, it will return the location of java.exe
.
C:\WINDOWS\system32> where java
C:\Users\User1\.jdks\openjdk-15.0.1\bin\java.exe
Use which java
to Check for Java Installation in Mac
The equivalent of the where
command in Mac is which
that does the same operation as where
and returns the file’s location. We use the command which java
in the terminal of the Mac device, and if Java is installed, it returns the location of the Java.
> which java
/Users/User1/.jenv/shims/java
Use System.getProperty("java.version")
to Check Java Version in Java
We can check if Java is installed programmatically using the getProperty()
function of the System
class that returns system properties.
We create a Java program, and in the print
statement, we call the getProperty()
from the System
class and pass the java.version
as an argument.
In the output, we get the version of the installed Java that proves that a version of Java is installed on the machine.
public class ExampleClass1 {
public static void main(String[] args) {
System.out.println(System.getProperty("java.version"));
}
}
Output:
15.0.1
Note that the above steps might not work if we have not set Java’s path as a global variable.
Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.
LinkedIn