Java - What is the Classpath?

Java Conceptuel Diagram

About

Java classes needed to execute a Java program can be physically located in different file system directories.

When Java classes are loaded, it searches a list of directories that are specified in what's called the classpath.

It's the same notion as the PATH environment variable for executable but for Java class.

Classpath and Executable Jar

Using the -jar option of java disables the use of CLASSPATH.

It means that you can't modify the classpath of excutable jar if you use the -jar option to execute it.

How can I set

Do not put quotes, or trailing backslash in the CLASSPATH, even if there is a space in a path directory

The class path can be set using one of this method.

JVM cp Options

  • -classpath option (shortened -cp) when calling an SDK tool
sdkTool -classpath classpath1;classpath2...

The -classpath option is present in the following sdk (java, jdb, javac, and javah ). Example:

# Windows with ;
java -cp /directory/with/my/class;anotherDirToaJar path/To/MainClass 
# Linux with :
java -cp /directory/with/my/class:anotherDirToaJar path/To/MainClass 

Environment variable

By setting the CLASSPATH environment variable in a command line

set CLASSPATH=classpath1;classpath2... 

Do not ever set CLASSPATH for the system. It only causes confusion and breaks things. In most development environments, it is best to temporarily set the CLASSPATH environment variable in the command line shell.

Manifest file

In a manifest file

Class-Path: lib/supportLib.jar lib/supportLib2.jar

How can I add

a Class?

You need to add the directory root of the class definition.

Example for the class utility.myapp located in the directory c:\java\MyClasses\utility\myapp, you need to add the directory c:\java\MyClasses\

java -classpath C:\java\myClasses utility.myapp.Cool

a Jar?

  • Adding one jar

In an archive file (a .zip or .jar file) the class path entry is the path to and including the .zip or .jar file. For example, to use a class library that is in a .jar file, the command would look something like this:

java -classpath C:\java\myClasses\myClasses.jar utility.myapp.Cool
  • Adding all JAR files in a directory (since Java 6)
REM Windows
java -classpath ".;c:\mylib\*" MyApp
echo Linux example
java -classpath '.:/mylib/*' MyApp

How can I get the classpath value?

Java - System Property

System.getProperty("java.class.path")

Snippet

DOS: Building a classpath from a directory of library

@REM Appending additional jar files to the CLASSPATH...
IF EXIST %MY_DIR%\lib FOR %%G IN (%MY_DIR%\lib\*.jar) DO (CALL :APPEND_CLASSPATH %%~FSG)

echo %CLASSPATH%
java ....

:APPEND_CLASSPATH
SET CLASSPATH=%CLASSPATH%;%1
GOTO :EOF

Support

You cannot use both -jar and -cp on the command line

See http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/java.html#-jar

Documentation / Reference





Discover More
Card Puncher Data Processing
Ant - Classpath

Classpath in a Ant Context The classpath element is defined and has the scope of a task. where: the location attribute refer to a single file or directory relative to the project's base directory...
Idea Antlr Right Click Options
Antlr - Installation (Version 4)

Installation of Antlr tool on . on Idea Create a grammar file with the extension g4 and Idea should propose you to install the Antlr plugin Right click on your g4 file, you should see the...
Drill Web Console
Apache Drill

Drill is a SQL Engine aimed to be able to query data file stored in a file system structure. At the core of is the 'Drillbit' service, which is responsible for accepting requests from the client, processing...
Eclipse Add External Lib Classe12
Eclipse - How to add an archive to the classpath ?

How to add in the Classpath an archive. In this example a Jdbc Driver (JAR File). download the archive. For an Oracle Jdbc driver, you can found them on...
Fitnesse Architecture
Fitnesse - ClassPath

in Fitnesse Because Fitnesse variable are also Java proeprty, you can show the classpath everywhere with this code Fitnesse build its own internal class path recursively in the tree by concatenating...
Path Bin Derby Home
How to install Derby in a client/server mode with the Network server mode? (JavaDB)

Derby (ie Java DB) has two types environment and then two types of installation. This article goes over an installation in the client/server architecture with the installation of Java DB within the Network...
Hudson Job Ant Junit
Hudson - Ant Build Steps with Junit Test Call

This article shows you an example on how you can start your junit test with the Ant builder (runner) and Hudson as Scheduler and reporter. An Hudson Job can call an Ant script. Ant can call junit...
Ant Idea Cannot Resolve Scp
IDEA - Ant (Cannot resolve symbol sshexec, Scp)

This page will show you how to resolve the error: 'Cannot resolve symbol sshexec, Scp' in Idea
Jdbc Class Architecture
JDBC - Driver

JDBC drivers must implement the java/sql/DriverDriver interface, and the implementation must contain a static initializer that will be called when the driver is loaded. This initializer registers a new...
Jmeter Gui
JMeter - Installation (version 2.7)

How to install JMeter a JRE/JDK must be installed the JAVA_HOME environment variable must be set Create an installation directory where the directory path contains any spaces Unzip...



Share this page:
Follow us:
Task Runner