Categories
AI & Tech

How to set PATH and CLASSPATH in Windows and/or Unix?

What’s the difference between these two terms that we always refer to?

In this article, let’s try to understand what these two variables are
• PATH
• CLASSPATH

PATH and CLASSPATH are two most important environment variables of any system and thus also comes in use in Java environment. These variables are use to locate the JDK binaries used to compile and also to run Java in Windows and/or Linux.  It is a known fact that whenever you are trying to set up a new environment ( for example: Java), it is very important to have right things at the right place.

In this context,

let us try to understand what these variables are ?

How do we need to set them up for the very first time?

Let us also explore on what happens if this important step is not done as well. Most common cause of dreaded error like java.lang.NoClassDefFoundError and java.lang.ClassNotFoundException is always an incorrect CLASSPATH in Java.

How to set PATH and CLASSPATH in Windows and/or Unix?

Let’s dive into setting up these variables then:

The very first thing is to fire up a Command Prompt on Windows or a Shell prompt on Linux to set these variables. 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 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

Mind you, in Linux the separator would be a colon (:) instead of semi-colon (;) in Windows.

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

Do ensure to include the current directory which is denoted by a dot (.) to be included in the CLASSPATH to ensure that it is needed to look in the Current Directory. (Let’s say if the same class/file/jar is available on a different folder structure then it will skip it and instead use the one available in the Current Directory.)

You need to set up your JAVA_HOME along with the other two parameters, namely PATH and CLASSPATH for your Java programs to work.

This is how you set your JAVA_HOME variable:

This is how you set your PATH variable, pointing to your JAVA_HOME variable.

CLASSPATH should be containing the locations to any jar(s) files that would be required within the compilation of your Java program(s).

Exit mobile version