What is classpath environment variable in java, how to configure it from CMD with example program

You are here : Home / Core Java Tutorials /

Contents of page >
  • What is classpath in java?
  • What is location ?
  • What if class is not available in classpath in java?
  • How to configure classpath in CMD(command prompt) ?
    • Then we will set environment variables
    • Setting temporary JAVA_HOME & PATH  >

  • Set the CLASSPATH>
  • What if CLASSPATH already exists >
  • How to unset the CLASSPATH>
  • What are third party classes/jars/libraries?
  • What are user defined classes/jars/libraries?
  • Example program to use -classpath command to use third party library like Oracle jar
  • How to configure classpath in eclipse ?

What is classpath in java?
A classpath environment variable is the location from which classes are loaded at runtime by JVM in java.
Classes may may include system classes and user-defined classes.

What is location ?
Locations is directory which may consist of class files or jar files(jar consists of class files).

What if class is not available in classpath in java?
ClassNotFoundException is thrown when JVM tries to class from classpath but it does not find that class.

How to configure classpath in CMD(command prompt) ?
First we will set the JDK path in windows >

What is Path? A path is a unique location of a file/ folder in a OS. PATH variable is also called system variable or environment variable.

Go to My Computer, right click on properties

Click Advanced System setting, Environment Variable, New..



Then we will set environment variables
  1. JAVA_HOME &
  2. PATH  >


1. set JAVA_HOME >
Variable name : JAVA_HOME
variable value: C:\Program Files\Java\jdk1.7.0_51 (Directory in which java has been installed)

Click OK.


2. set PATH  >
Again click on New..
Then enter -
Variable name : PATH
variable value: C:\Program Files\Java\jdk1.7.0_51\bin
Click OK.



Setting temporary JAVA_HOME & PATH  >
open cmd
type
set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_51
set PATH=C:\Program Files\Java\jdk1.7.0_51\bin

Once you will exit cmd JAVA_HOME and PATH will be lost.



After setting classpath, you can use commands like java, javac, javap and many more in CMD.

Then, You can use following command to add more user defined or third party classes (libraries)>
java -cp
java -classpath


Set the CLASSPATH>
Syntax -
set CLASSPATH=.;jarLocation  
Example -
set CLASSPATH=.;E:\java jars\ojdbc14.jar

You can as many jars as you want to add by using ; as separator.

What if CLASSPATH already exists >
Syntax -
set CLASSPATH=%CLASSPATH%;E:\java jars\ojdbc14.jar

How to unset the CLASSPATH>
Syntax -
set CLASSPATH=
Example -
set CLASSPATH=


What are third party classes/jars/libraries?
Third party classes (libraries) may include Oracle, MySql, postgreSql jars etc.

What are user defined classes/jars/libraries?
User defined classes (libraries) may include libraries created by user. Example - MyClasses.jar




Example program to use -classpath command to use third party library like Oracle jar

package myPackage;
public class ClasspathSetupOracle {
public static void main(String... arg) {
     System.out.println("register Oracle driver class, i.e. initialize OracleDriver");
     try {
          Class.forName("oracle.jdbc.driver.OracleDriver");
          System.out.println("oracle.jdbc.driver.OracleDriver loaded");
     } catch (ClassNotFoundException e) {
          e.printStackTrace();
     }
}
}


Let’s say above java file is in directory=  E:\workspace\MyProject\src\myPackage\ClasspathSetupOracle.java

We can create .class file by cleaning and building eclipse project.

Let’s clean and build the eclipse project >

When project is clean and builded using eclipse, .class file will be formed in directory=  E:\workspace\MyProject\bin\myPackage\ClasspathSetupOracle.class

Let’s try to execute above program from CMD,  Go to CMD, go to .class file directory and type below command >

E:\workspace\MyProject\bin>java myPackage.ClasspathSetupOracle
register Oracle driver class, i.e. initialize OracleDriver
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
       at java.net.URLClassLoader.findClass(Unknown Source)
       at java.lang.ClassLoader.loadClass(Unknown Source)
       at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
       at java.lang.ClassLoader.loadClass(Unknown Source)
       at java.lang.Class.forName0(Native Method)
       at java.lang.Class.forName(Unknown Source)
       at myPackage.ClasspathSetupOracle.main(ClasspathSetupOracle.java:7)

At runtime JVM will search for oracle.jdbc.driver.OracleDriver  class in classpath. But, ClassNotFoundException occurred because ojdbc.jar wasn’t present in the classpath.

For solving this Error, first we need to set the classpath, then execute the program >
E:\workspace\MyProject\bin>set CLASSPATH=.;E:\java jars\ojdbc14.jar

E:\workspace\MyProject\bin>java myPackage.ClasspathSetupOracle
register Oracle driver class, i.e. initialize OracleDriver
oracle.jdbc.driver.OracleDriver loaded


And problem is resolved, class file is executed successfully!



How to configure classpath in eclipse ?
Using eclipse is the easiest of the ways to configure classpath in java.

First java Program to connect to Oracle database.

Right click on Java project in eclipse, and click properties
Select Java build path, click add external jar, give path of ojdbc6.jar, click Ok.


Java Program to connect to Oracle database - Load ojdbc6.jar from classpath >
package myPackage;
public class ClasspathSetupOracle {
public static void main(String... arg) {
     System.out.println("register Oracle driver class, i.e. initialize OracleDriver");
     try {
          Class.forName("oracle.jdbc.driver.OracleDriver");
          System.out.println("oracle.jdbc.driver.OracleDriver loaded");
     } catch (ClassNotFoundException e) {
          e.printStackTrace();
     }
}
}


At runtime JVM will search for oracle.jdbc.driver.OracleDriver  class in classpath(build path).




Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


Related >>

Resolve Error: Could not find or load main class


error: Class names are only accepted if annotation processing is explicitly requested in java: solution

No comments:

Post a Comment