PATH and CLASSPATH in Java

Connect with

PATH and CLASSPATH in JavaLearn how to set environment variable of PATH and CLASSPATH in Java on Linux, Windows operating system. Different aspects of PATH and CLASSPATH environment variables.

1. PATH and ClASSPATH in Java

As per my personal experience, I can say that PATH and CLASSPATH are the two most problematic things for beginners in the Java programming language. Most of Java courses, does not says details of what is a PATH and CLASSPATH? What PATH and CLASSPATH do? Why did we require to set variables for PATH and CLASSPATH? What happens if we do not set these environment variables? How does Classpath work in Java? Difference between PATH vs CLASSPATH in Java? These basic and funny questions can be asked by yourself.

2. PATH environment variable

  • 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.

3. CLASSPATH environment variable

  • 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.

4. 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:

4.1 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

4.2 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.

5. Set environment variable Permanently

5.1 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.

5.2 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

6. Difference between PATH and CLASSPATH in Java

  1. The main difference between PATH and CLASSPATH is, PATH is an environment variable that is used to locate JDK binaries like “java” or “javac” command used to run java program and compile the 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 the operating system to find any binary or command typed in the shell/terminal, in Windows and Linux environments. 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.

7. Reference

Oracle Java PATH and CLASSPATH
I hope you enjoyed this post about how to set environment variables PATH and CLASSPATH in Java, and you can visit Core Java tutorial for more blog post.
Your suggestions or comments are welcome to improve this post. Happy Learning! 🙂


Connect with

Leave a Comment

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