If you want to execute Java programs, you need to install Java Development Kit(JDK) or Java Runtime Environment(JRE) and include the Java Executable location to the Path Environment Variable. Modern Java installers do this automatically.
How to check if Java is already installed and the Path variable has been set?
You can manually check if Path variable has been set once the installation is complete by following this simple method:
1. Open Command Prompt(Windows) or Terminal(Mac/Linux) window.
2. Enter `java -version` and press Enter.
If this command returns a version number, Java is installed and the Path environment has been set. Otherwise, an error message will be shown and you might need to reinstall Java or set the Path variable as given below.
Note: You may either set the User-level Path variable or the System-wide Path variable. If the System-wide Path variable is set, you won't have to do the same for other User Accounts on the machine.
What are Environment Variables - Path, JAVA_HOME, and JRE_HOME?
JAVA_HOME - This variable points to the base directory of the JDK installation in your system. This is the folder which contains the 'bin' and 'lib' folders for Java.
JRE_HOME - This variable points to the base directory of the JRE installation in your system.
Path - The Path Environment variable helps the system to determine search path for commands. When a command is executed, the system will search in these locations for an executable file with the name of the command. Therefore, if we need to run Java programs from anywhere in the System, we need to add the "Java binary folder location" to the Path variable.
Note: Path variable will be automatically set by the Java Installer.
Finding Java Installation location
Generally, the java_installation_base_folder location is C:\Program Files\Java\jdk<version>
For 64 bit machines, the java_installation_base_folder location is C:\Program Files (x86)\Java\jdk<version> or the above one.
For Linux, you might be using either OpenJDK or Oracle JDK. You can use the whereis java command to find java executable location on your Linux machine.
For OpenJDK, The java_installation_base_folder location is similar to /usr/lib/jvm/openjdk/jdk<version>
For Oracle JDK, The java_installation_base_folder location is similar to /usr/lib/jvm/java-8-oracle/
You can use /usr/libexec/java_home command to find java installation folder on your Mac.
Note: There might be slight changes in your installation location according to the version of Java installed and your OS version. That's why it is recommended to check the java installation location before setting the Path variable.
Setting the System Environment Variables
After getting the installation location using the where command,
1. Open Command prompt(CMD) program with Administrator privileges.
2. Enter the following command with your java base directory location and press Enter key.
setx /m JAVA_HOME "<java base directory location>" setx /m JDK_HOME "<java base directory location>" setx /m JRE_HOME "<java base directory location>\jre"
Check setx /? for more help.
For Java 8 on Windows 10, we have found that the java installation location is "C:\Program Files\Java\jdk1.8.0_144"
Therefore the format of the setx command would be,
setx /m JAVA_HOME "C:\Program Files\Java\jdk1.8.0_144" setx /m JDK_HOME "C:\Program Files\Java\jdk1.8.0_144" setx /m JRE_HOME "C:\Program Files\Java\jdk1.8.0_144\jre"
After getting the installation location using the whereis or locate command,
1. Open Terminal window and type the following command to open the .profile file in nano editor
This will open the .profile file in the terminal window.
2. Scroll down to the end of the file and add the following line at the end.
export JAVA_HOME=<java_installation_location> export JDK_HOME=<java_installation_location> export JRE_HOME=<java_installation_location>/jre
export JAVA_HOME=/usr/java/jdk1.5.0_07 export JDK_HOME=/usr/java/jdk1.5.0_07 export JRE_HOME=/usr/java/jdk1.5.0_07/jre
3. Press Ctrl+O to write it to the file and confirm the changes.
Note: For Mac, you may need to enable locate command by executing the following command in Terminal.
sudolaunchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
Here's another detailed article from java.com on setting up the Environment variables for Java in Windows/Mac/Linux - How do I set or change the PATH system variable?