Introduction
PATH is an environment variable which is used by the operating system to find out an executable so that it can execute it. Whenever we are trying to execute any OS command or any customised program and we type the name of the command or the program in the shell , then the OS searches for the command in the PATH environment variable’s content. Normally PATH environment variable contains a list of colon (for Unix & MacOS) or semicolon (for windows) separated folders and the OS searches the location of the command / program from the left to the end of the list of folders. Hence some folders which contains the OS commands are always present in the PATH environment variable.
Why and how can we modify PATH ?
Normally the content of the PATH environment variable is set at the system level but we can also modify it for a shell temporarily. It is modified temporarily for example to set a different version for a software at some point of time. For example, I often use the below script to switch between 2 versions of Java software in my local OS. Advantage of this scripted approach is that, it does not change the system variable’s value – it changes the value in a local shell temporarily and the change will be lost when we exit the shell. So I follow the below steps to execute the script below:
- Copy the content of the below script in a file named say setJDKLocally.sh
- I can then open a shell in Linux / Mac and execute the command : source setJDKLocally.sh
- When the above command is run, it sets JDK to version 22. Again when I modify the script and comment the 1st line and uncomment the 2nd line and rerun the command ‘source setJDKLocally.sh’, then it sets JDK to GraalVM 22 which is a different version of JDK.
export JAVA_HOME=/Users/msoumik/softwares/jdk22/jdk-22.jdk/Contents/Home
#export JAVA_HOME=/Users/msoumik/softwares/graalvm/graalvm-jdk-22.0.1+8.1/Contents/Home
export MAVEN_HOME=/Users/msoumik/softwares/personalMaven/apache-maven-3.8.5
export PATH=$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH
We can also write a similar script in Windows with the below content copied in a file named say setJDKManually.bat and run it using the command :
set JAVA_HOME=C:/msoumik/softwares/jdk22/jdk-22.jdk/Contents/Home
#set JAVA_HOME=C:/msoumik/softwares/graalvm/graalvm-jdk-22.0.1+8.1/Contents/Home
set MAVEN_HOME=C:/msoumik/softwares/personalMaven/apache-maven-3.8.5
set PATH=%JAVA_HOME%/bin;%MAVEN_HOME%/bin;%PATH%
A few things which differentiates the windows script from the Linux/MacOS scripts are as follows :
- In Linux/Mac, environment variables are invoked using the $ sign and in windows, they are invoked using the %% sign
- In Linux/Mac, the list of folders (which is the value of the variable) are colon de-limited while in windows, they are semi colon de-limited
- Further in windows, it is better to use forward slash
Classpath : what is it ?
Classpath is a concept in the Java world and it is very similar to the PATH environment variable. Just like the OS searches for the location of a command in the list of folders denoted by PATH variable, the JVM searches for a class file to run from a list of folders (and jar files) denoted by the CLASSPATH environment variable. We can also set classpath environment variable manually while running a java program. So below is a command to run a java program by searching the location of class file from the classpath environment variable (-cp denotes the classpath and the location of the classpath is 2 folders : ‘/Users/msoumik/software/programs’ and the current folder denoted by period (.) ):
java -cp /Users/msoumik/software/programs:. TestProgram
In windows, the above command would look like : java -cp C:/Users/msoumik/software/programs;. TestProgram. Note the forward slash (/) in windows and semicolon separator (instead of the colon separator).
Conclusion
So now that we have seen the concept of the PATH and the CLASSPATH environment variables, please go ahead and create a script like I have shown