Saturday, August 18, 2012

What is PATH and CLASSPATH in Java - Path vs ClassPath

What is PATH and CLASSPATH in Java
PATH and CLASSPATH are two most important environment variable of Java environment which is used to find JDK binaries used to compile and run Java in windows and Linux and class files which are compile Java byte codes. From my personal experience I can say that PATH and CLASSPATH are two most problematic thing for beginners in Java programming language due to two reasons; first because in most of Java courses nobody tell details of What is PATH and CLASSPATH, What does PATH and CLASSPATH do, What is meaning of setting PATH and CLASSPATH, What happens if we do not set them, Difference between PATH vs CLASSPATH in Java or simply How Classpath works in Java etc. These basic question which answers most of details about PATH and CLASSPATH in Java are mostly not answered until Java programmer itself acquire these knowledge, Things may been changed now days but important of PATH and CLASSPATH is still high. Most common cause of dreaded error like java.lang.NoClassDefFoundError and java.lang.ClassNotFoundException are incorrect or misconfigured CLASSPATH in Java.



Difference between PATH and CLASSPATH in Java

Here are some of the common difference between PATH vs CLASSPATH in Java :

1)Main difference between PATH and CLASSPATH is that  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. On the other hand CLASSPATH environment variable is used by System or Application ClassLoader to locate and load compile Java bytecodes stored in .class file.

2) In order to set PATH in Java you need to include JDK_HOME/bin directory in PATH environment variable while in order to set CLASSPATH in Java you need to include all those directory where you have put either your .class file or JAR file which is required by your Java application.

3) Another significant difference between PATH and CLASSPATH is that 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 shell, this is true for both Windows and Linux environment while CLASSPATH is only used by Java ClassLoaders to load class files.

These were some notable difference between PATH vs CLASSPATH in Java and they are worth remembering to debug and troubleshoot Java related issues.

How to set PATH and CLASSPATH in Windows and Unix
What is PATH and CLASSPATH in Java , Difference between themIf you are familiar with DOS operating system and how to use command prompt in Windows or shell in Linux setting PATH and CLASSPATH is trivial exercise. Both PATH and CLASSPATH are environment variable and can be set using export in Linux and using set keyword in DOS and Windows as shown below:

Command to set PATH in Windows

set PATH=%PATH%;C:\Program Files\Java\JDK1.6.20\bin

Command to set PATH in UNIX/Linux

export PATH = ${PATH}:/opt/Java/JDK1.6.18/bin

Look at the difference between two commands, in Linux use colon(:) as separator and in Windows use semi-colon(;) as separator.

Command to set CLASSPATH in windows

set CLASSPATH=%CLASSPATH%;C:\Program Files\Java\JDK1.6.20\lib

Command to set CLASSPATH in Unix/Linux

export CLASSPATH= ${CLASSPATH}:/opt/Java/JDK1.6.18/lib

Also don't forget to include current directory, denoted by dot(.) to include in CLASSPATH, this will ensure that it will look first on current directory and if it found the class it will use that even if that class also exists in another directory which exists in CLASSPATH.

13 comments:

  1. Best explanation of ClassPath and Path I ever read , Thanks for this simple yet informative tutorial on Java.

    ReplyDelete
  2. Nice Explanation Sir.This is really very beneficial for beginners .

    ReplyDelete
  3. Hye javin.
    I would like to ask. What does the last line means?

    ReplyDelete
  4. I Love u man. U are simply Great

    ReplyDelete
  5. Java uses computer file system directories to store packages. For example, the .class files for any classes you declare to be part of Wisen must be stored in a directory called Wisen.

    Java is case sensitive. Therefore, the directory name must match exactly the package name even in Windows operating system.


    Java Training In Chennai

    ReplyDelete
  6. Thats great tutorial... Many many thanks to you Sir.. This help me a lot..

    ReplyDelete
  7. Hello, World
    Exception in thread "main" java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:434)
    at java.util.Properties.load0(Properties.java:353)
    at java.util.Properties.load(Properties.java:341)
    at FileHelper.getFilePathToSave(FileHelper.java:19)
    at FileHelper.main(FileHelper.java:33)

    ReplyDelete
  8. Nice explaination.
    I am using JDK 7.I have set PATH but not set CLASSPATH and thing are working fine.
    Question is if we don't set CLASSPATH explicitly what is assumed by java ?

    ReplyDelete
  9. export CLASSPATH= ${CLASSPATH}:/opt/Java/JDK1.6.18/lib

    the space following = should be removed

    ReplyDelete
  10. @ : Question is if we don't set CLASSPATH explicitly what is assumed by java ?

    For running java programs it not necessary to set CLASSPATH. It is necessary only when you use classes from third party .jar files. Here in this example I am taking the example of JDBC driver for MySQL.

    ReplyDelete
  11. Thank you,nice way of description of Difference between Path and Class Path. It is one of the most frequently asked question in java For Freshers. Setting Path for File JDK Software and Setting Classpath for Jars Class names of Servlet Struts, Hibernate, Spring

    ReplyDelete
  12. Thank you for your great explanation.

    ReplyDelete
  13. why do u need to set ClassPath when u use -cp option?

    ReplyDelete

Java67 Headline Animator