[java] How to check whether java is installed on the computer

I am trying to install java windows application on client machine.I want to check whether requried JRE is installed on the machine or not. I want to check it by java program not by cmd command

This question is related to java runtime

The answer is


You can do it programmatically by reading the java system properties

@Test
public void javaVersion() {
    System.out.println(System.getProperty("java.version"));
    System.out.println(System.getProperty("java.runtime.version"));
    System.out.println(System.getProperty("java.home"));
    System.out.println(System.getProperty("java.vendor"));
    System.out.println(System.getProperty("java.vendor.url"));
    System.out.println(System.getProperty("java.class.path"));
}

This will output somthing like

1.7.0_17
1.7.0_17-b02
C:\workspaces\Oracle\jdk1.7\jre
Oracle Corporation
http://java.oracle.com/
C:\workspaces\Misc\Miscellaneous\bin; ...

The first line shows the version number. You can parse it an see whether it fits your minimun required java version or not. You can find a description for the naming convention here and more infos here.


If java not installed yet. Then program written by java cannot be run to check if java is installed or not.


Type in the command window

java -version

If it gives an output everything should be fine. If you want to develop software you might want to set the PATH.


Go to this link and wait for a while to load.

http://www.java.com/en/download/testjava.jsp

You will see the below image: enter image description here

You can alternatively open command window and type java -version


After installing Java, set the path in environmental variables and then open the command prompt and type java -version. If installed properly, it'll list the java version, jre version, etc.

You can additionally check by trying the javac command too. If it displays some data, you've your java installed with the proper path set, else it'll that javac is an invalid command.


Using Apache Commons-Lang's SystemUtils.isJavaVersionAtLeast(JavaVersion)

import org.apache.commons.lang3.JavaVersion;
import org.apache.commons.lang3.SystemUtils;

if (SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_8)
    System.out.println("Java version was 8 or greater!");

Check the installation directories (typically C:\Program Files (x86) or C:\Program Files) for the java folder. If it contains the JRE you have java installed.


command prompt:

C:\Users\admin>java -version (Press Enter>
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)

Open Command Prompt and type in the following command: java -version

Upon successful execution, the command will output the version of Java along with Java SE Runtime Environment’s build and Java HotSpot Client VM’s build.

enter image description here


1)Open the command prompt or terminal based on your OS.

2)Then type java --version in the terminal.

3) If java is installed successfullly it will show the respective version .


type java -version in command prompt, it will give you the installed version of java on your system.