How to Set classpath to Import JAR Files

Let’s see how to set classpath and why there is a need to set the classpath.

The classpath is the path that the Java runtime environment searches for classes and other resource files. It tells JDK tools and applications where to find “third-party and user-defined classes” i.e classes that are not Java extensions or part of the Java platform.

Suppose you are working on a java project “using notepad or without using any IDE” than to make the additional necessary jar files available to your project, you need to set each jar file by giving full path of the jar file in the classpath.

You can define your all the jar file once and no need to give classpath every time you start command prompt, this can be done as follows:

  • Right click on my computer —>  properties —>  advanced system settings —> Environment variables   —> Click on “New” below user variables.

classpath

  • Enter Variable name as classpath
  • Enter Variable value as specify complete path like C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.jar;

classpath1

  • click on ok button

classpath2

  • Now you will see a new variable with name classpath and in value the specified path of jar file with name
  • If you want to add another jar, for this click edit —> add semicolon <;> then the path of other jars, like C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.jar;.;C:\abc.jar;

This way you can set the classpath.