Configuring the CLASSPATH - Java Language Basics

Java examples for Language Basics:Class Path

Introduction

Set the CLASSPATH variable equal to the directory location of the user-defined Java classes or Java Archive (JAR) files.

set CLASSPATH=C:\JAVA_DEV\some-jar.jar 
 

Or on Unix and Linux systems:

export CLASSPATH=/JAVA_DEV/some-jar.jar 
 

javac command provides an option for specifying the location of resources that need to be loaded for an application.

On all platforms, setting the CLASSPATH using javac can be done via the -classpath option as follows:

javac -classpath /JAVA_DEV/some-jar.jar 
 

on Microsoft Windows machines the file path will use the backslash (\) instead.

javac -cp option may be used, rather than specifying the -classpath option.

JVM finds and loads classes as needed using the following search order:

  1. The classes are contained in the Java installation directory.
  2. Any packages or JAR files that are located within the extension directory of the JDK.
  3. Packages, classes, JAR files, and libraries that are loaded on the specified class path.

Following is an example of specifying multiple JAR files in the CLASSPATH environment variable on Unix and Linux systems:

export CLASSPATH=/JAVA_DEV/some-jar.jar:/JAVA_LIB/myjar.jar 
 

Alternatively, you can specify the class path via a command-line option:

javac -classpath /JAVA_DEV/some-jar.jar:/JAVA_LIB/myjar.jar

Related Tutorials