PATH & CLASSPATH environment variable in Java

Wikitechy | 4431 Views | java | 13 Jun 2016

 

  • PATH and CLASSPATH are two environment variable which plays the main role on Java development environment. 

PATH:

  • PATH environment variable is used to locate binary or Java commands required to compile and execute Java program like javac ( java compiler) and java command which must be in PATH in order to compile any Java file and run any Java program. 

Class PATH:

  • CLASSPATH is used to find .class file which is loaded by Application ClassLoader. 
  • There are three types of Class loaders that are used to locate and load class files 

1. Boot Strap class loader, 

2. extension class loader and 

3. System or application class loader. 

  • These three class loader are responsible for loading classes from CLASSPATH in Java.
  • Both PATH and CLASSPATH environment variable contains of list of directories where Operating System like Windows, Unix, Linux or Solaris and JVM search for Java binaries and class files.
  • If PATH is not set then it means that, Java binaries are not present in PATH environment variable, so we cannot run java and javac command where we can receive an error "javac unknown command or command not found".

What happens if CLASSPATH and PATH is not set in Java?

While CLASSPATH is not set then it means that we need to get an java.lang.ClassNotFoundException (or) java.lang.NoClassDefFoundError in Java . Based upon the corresponding class, CLASSPATH will present on compile time where it will not present on runtime.

How to set Environmental variable in Windows?

1. Right click the Computer icon 

2. Choose Properties from the context menu.

3. Click the Advanced system settings link.

4. Click the Environment Variables in the section System Variables, find the PATH environment variable and select it. 

5. Click Edit. If the PATH environment variable does not exist, click New.

6. In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. 

7. Click OK. Close all remaining windows by clicking OK.

How to set PATH and CLASSPATH in Windows?

We can set the PATH and CLASSPATH in windows by executing the following command:

set CLASSPATH=%CLASSPATH%;C:\MyApplication\lib

%CLASSPATH% is used to get the value of current CLASSPATH variable and then it appends your application directory on CLASSPATH. 

Similarly, PATH can be set in Windows by using following command:

set PATH=%PATH%;C:\Program Files\Java\jdk1.6.0\bin

How to set CLASSPATH and PATH in Linux/Unix/Solaris?

Similar to the technique we used to set CLASSPATH and PATH in windows by command prompt, we can set CLASSPATH in any UNIX environment like RedHat Linux, IBM AIX or Sun Solaris.

export CLASSPATH= ${CLASSPATH}:/home/user/java/JDK1.6/lib

${CLASSPATH} is used to refer value of old CLASSPATH environment variable. 

Similarly, to set PATH in UNIX or Linux just replace CLASSPATH with PATH environment variable as shown in below example:

export PATH= ${ PATH }:/opt/ Java/jdk1.6.0/bin

Applies to:

  • J2SE 1.3
  • J2SE 1.4
  • J2SE 5.0
  • Java SE 6
  • Java SE 7
  • Java SE 8
  • UNIX 
  • RedHat Linux, 
  • IBM AIX or Sun Solaris.

Related Tags:

  • PATH and CLASSPATH
  • Set the CLASSPATH environment variable
  • Environment Variables for Java Applications - PATH, CLASSPATH 
  • What is PATH and CLASSPATH in Java - Path vs ClassPath
  • How to Set Java Class Path Permanently in Windows 7
  • Setting Environment Variables JAVA_HOME, PATH and CLASSPATH
  • How to set path and classpath variable for java in Windows 7
  • How to set environment variables in Windows 7 for Java
  • Environment Variables
  • How to Set Classpath for Java on Windows and Linux
  • java classpath environment variable windows
  • java classpath environment variable windows 7
  • java classpath environment variable linux
  • after installing java set path
  • how to set classpath in java in windows 7
  • difference between path and classpath in java
  • java classpath command line
  • how to set java path in windows 7



Workshop

Bug Bounty
Webinar

Join our Community

Advertise
<