Once the JDK installation is complete, the next big step is setting the Environment Variables in your machine. Environment Variables setting is different for Windows and Mac machines. I will walk you through in setting up the Environment Variables in both machines.
Before setting up the Environment Variables, let me give you a basic idea on what are Environment Variables?
Environment variables are, in short, variables that describe the environment in which programs run in.Examples of environment variables in an operating system include PATH, HOME etc.
To execute JAVA console based programs in Windows or Mac environment we have to use javac and java commands. These commands are unknown for Windows or Mac till we do not specify explicitly where those executable resides. This is the reason while setting the PATH we specify path of bin folder(bin contains all the binary executable).
Mac OS:
- Open Terminal in Mac OS similar to command prompt in Window
- Type in the following command “vim .bash_profile”. This will open the VIM editor in terminal. bash_profile is a configuration file for bash shell. When .bash_profile is invoked as an interactive login shell it first reads and executes commands from ~/.bash_profile. This file can be used to export variables in shell.
- Type ‘i’ to insert into the VIM Editor.
- Type in “export JAVA_HOME=$(/usr/libexec/java_home)” to set up JAVA_HOME.
- Type in “export PATH=${JAVA_HOME}/bin:$PATH” to set up PATH.
- To Save the entires in VIM Editor, hit Escape, type in: “:wq + Enter”. To Quit without saving the editor, hit Escape, type in “:q!”.
- Type in “source .bash_profile”. This command will save the entries in .bash_profile.
- Type in “echo $JAVA_HOME” to verify JAVA_HOME Environment Variable.
- Type in “echo $PATH” to verify PATH Environment Variable.
Please ignore M2_HOME as of now. This is for setting up the MAVEN Home which I will explain in future blogs.
Windows OS:
- Type “advanced system settings” in the search box (beside the Windows start button), clicks View advanced system settings.
- Select Advance tab, clicks Environment Variables.
- In System variables, add a new “JAVA_HOME” variable and point it to the JDK installed folder which by default is “C:\Program Files\Java\jdk1.8.0_111”.
- In System variables, find “PATH”, clicks edit and append this “%JAVA_HOME%\bin” to the end.
- Type in “echo %JAVA_HOME%” to verify JAVA_HOME Environment Variable.
- Type in “echo %PATH%” to verify PATH Environment Variable.
Now that we have completed the Environment Variable settings in our machine, we can move on to the next topic…
Please note that you need to be an Admin in your machine or else you won’t have the privilege to set the Environment Variables.