PATH and CLASSPATH in Java

Connect with

Oracle Java
In any programming language environment variables play an important role, specially in java. Few environment variables plays very much like PATH and CLASSPATH, these are two most important environment variables of Java environment which is used to find something by OS or JAVA.

As per my personal experience, I can say that PATH and CLASSPATH are two most problematic thing for beginners in Java programming language . Most of Java courses tutorial does not says details of what is a PATH and CLASSPATH? What do PATH and CLASSPATH do? What is meaning of setting PATH and CLASSPATH? What happens if we do not set them? How Classpath works in Java? Difference between PATH vs CLASSPATH in Java? These basic and funny questions can be asked by yourself .

PATH:

  • path variable is set to lookup the utility tool like like java, javac, wsgen etc.
  • The best way to set PATH is, first set JAVA_HOME and then include bin folder after that, so that utility tool can be execute from any folder in the respective OS ( Window or Linux, Mac, Solaris) else you have to go to that folder where javac or utilities are.

CLASSPATH:

  • used by class loader to load our application .classes.
  • usually , in JAVA IDE we use to include .jar by any build tool maven , ant or manually by build path.

How to set Path and CLASSPATH variables in different OS

In DOS/Windows operating system use “set” and in Linux/Mac use “export“. Both PATH and CLASSPATH are environment variable and can be set using export command in Linux and using set keyword in DOS and Windows as shown below:

Command to set PATH and CLASSPATH in Windows

set PATH=%PATH%;C:\Program Files\Java\JDK7\bin
set CLASSPATH=%CLASSPATH%;C:\Program Files\Java\JDK7\lib

Command to set PATH in UNIX/Linux

export PATH=$PATH:/opt/Java/JDK7/bin
export CLASSPATH= ${CLASSPATH}:/opt/Java/JDK7/lib

NOTE:

  1. variable set by command prompt is temporarily, so in order to set permanently.
  2. Look at the difference between two commands, in Windows use semi-colon(;) as a separator and in Linux use a colon(:) as a separator.
  3. To get the already set environment variable , use droller ($) prefix in Linux/Unix/Mac and use % (percentage symbol) prefix and suffix to get value of already set variable.
  4. set JAVA_HOME before bin folder and then append bin folder after that , it means any HOME either M2_HOME or CASSANDRA_HOME or HADOOP_HOME environment variable is just before bin folder.

Set environment variable Permanently

In Windows:
select My computer -> click properties -> click Advance System Setting -> select Advance tab -> click Environment Variables -> under the system variable click New/edit and provide key and value for that.

In Linux:
add following line at end of the any of of the file: /etc/profile or ~/.bashrc or /.profile

vim /etc/profile

export JAVA_HOME=/opt/Java/JDK7
export PATH=$PATH:$JAVA_HOME/bin

Difference between PATH and CLASSPATH in Java

  1. The main difference between PATH and CLASSPATH is, PATH is an environment variable which is used to locate JDK binaries like “java” or “javac” command used to run java program and compile java source file. CLASSPATH, an environment variable is used by System or Application ClassLoader to locate and load compile Java bytecodes stored in the .class file.
  2. In order to set PATH in Java, you need to include JAVA_HOME/bin directory in PATH environment variable , while for CLASSPATH in Java you need to include all those directories where you have put either your .class file or JAR file which is required by your Java application.
  3. PATH can not be overridden by any Java settings but CLASSPATH can be overridden by providing command line option -classpath or -cp to both “java” and “javac” commands or by using Class Path attribute in Manifest file inside JAR archive.
  4. PATH environment variable is used by operating system to find any binary or command typed in the shell/terminal, in Windows and Linux environment. if you want to execute any utility tool like “javac” , “mvn” in maven you need to set in path environment variable, while CLASSPATH is only used by Java ClassLoaders to load class files.

Connect with

Leave a Reply

Your email address will not be published. Required fields are marked *