When you need to invoke java command after install JDK successfully, it is better to setup below Java related system environment variable, they are JAVA_HOME, CLASSPATH, PATH. This article will tell you how to set them up in windows and Linux.
JAVA_HOME, CLASSPATH, PATH Overview
JAVA_HOME is usually means the JDK installation home, such as C:\Java\jdk1.8.0_131
. It is not the JRE installation home. JRE_HOME is the environment variable which store java runtime environment installation path such as C:\Java\jre8
.
CLASSPATH is the path where to find the java class when your application executes. It’s value commonly be multiple jar file path separated by OS path separator(windows is ; and Linux is 🙂 . You can also use -cp to specify class path when run applications such as java -cp "abc.jar;sendMail.jar" com.dev2qa.SendMail
this will make the two jars available only to the execution process. If you set them in CLASSPATH system environment variable, it will be available to all java process. Since java 6, you can use wildcard * to include all the jar files under a folder to class path for example java -cp "sendMail.jar;lib/*" com.dev2qa.SendMail
.
PATH is the convenience way for run java command. It’s value always be JAVA_HOME\bin
. If you do not set the bin folder in PATH, you can call java command with C:\%JAVA_HOME%/bin/java -version
, after setup bin folder in PATH, you can easily call java command with C:\java -version
.
Setup JAVA_HOME, CLASSPATH and PATH For Windows
- For windows. Click ” Start Menu —> Control Panel —> System and Security —> System” to open below dialog.
- Click “Advanced system settings —> Environment Variable”, then a dialog will popup, you can add or update all system environment variable there. For example :
JAVA_HOME
, value :C:\Java\jdk1.8.0_131
.CLASSPATH
, value :mail.jar;selenium-standalone.jar
.PATH
, value :%SystemRoot%\system32;%JAVA_HOME%\bin
.
Setup JAVA_HOME, CLASSPATH and PATH For Linux
- Open /etc/profile in text editor,
# vi /etc/profile
. - Add
export JAVA_HOME=/usr/jdk1.8.0
. - Add
export CLASSPATH=$CLASSPATH:/home/LOG4J_HOME/log4j-2.2.16.jar:.
. - Add
export PATH=$PATH:/usr/jdk1.8.0/bin
. - Run
# . /etc/profile
to active above settings immediately. - Execute
# java -version
to verify whether the settings is correct or not.