Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   5. Java in Practice  >   5.7. Classpath

5.7. Classpath
Last updated: 27 January 2013.

There are situations where an application needs to use class files and resources that are not located in the directory tree where the application's files reside. In that case, those classes and resources will not be found unless you set the classpath for your application. The classpath is a variable that specifies where to find the class files and resources outside the application's directory tree. You can set the classpath in two ways:Using the -classpath command-line option affects applications separately whereas setting the CLASSPATH environment variable affects all the applications, which is why the recommended way of setting the classpath is to use the -classpath command-line option.


5.7.1. How to use the -classpath command-line option

To compile a class named App.java that uses class files outside its directory tree, the command would be the following on a Linux or Mac system:

javac   -classpath   path1:path2:path3   App.java

On a Windows system, the command would look like this:

javac   -classpath   path1;path2;path3   App.java

The paths path1, path2 and path3 are the absolute paths to the JAR files (with a .jar extension), class files (with a .class extension) or ZIP files (with a .zip extension) located outside the current directory tree. They are separated by colons on Linux and Mac systems. On Windows systems, they are separated by semicolons.

There are three kinds of paths:

In order to run the class App after having compiled it successfully, you would type a command like this on a Linux or Mac system:

java   -classpath   .:path1:path2:path3   App

On a Windows system, the command would be the following:

java   -classpath   .;path1;path2;path3   App

The current directory is the default classpath. Using the -classpath option overrides the default classpath. Therefore, the current directory (".") must be included explicitly in the classpath.

The order of the paths specified in the classpath is the order in which the class files will be searched. In the above example, the class files will be searched in the current directory ("."). If they are not found, then path1 will be searched and so on until the class files are found.

Note that with the java tool that runs applications, you can use the -cp option which is an abbreviation of the -classpath option as shown below:

java   -cp   .;path1;path2;path3   App



As an example, let's say the class App uses files outside its directory tree at the following locations:

/Users/yourName/utils/tools.jar
/Users/yourName/resources/utilities.zip
/Users/yourName/Projects/com/javaperspective/tutorials/Example.class

Hence, to compile the class App on a Linux or Mac system, the command is:

javac   -classpath   /Users/yourName/utils/tools.jar:/Users/yourName/resources/utilities.zip:/Users/yourName/Projects/   App.java

On a Windows system, the command would be:

javac   -classpath   /Users/yourName/utils/tools.jar;/Users/yourName/resources/utilities.zip;/Users/yourName/Projects/   App.java

The class Example belongs to the named package com.javaperspective.tutorials, which is why the path is /Users/yourName/Projects/. If the class Example was in an unnamed package, the path would have been /Users/yourName/Projects/com/javaperspective/tutorials/. To run the class App after a successful compilation, you would type the following command on a Linux or Mac system:

java   -classpath   .:/Users/yourName/utils/tools.jar:/Users/yourName/resources/utilities.zip:/Users/yourName/Projects/   App

On a Windows system, the command would be:

java   -classpath   .;/Users/yourName/utils/tools.jar;/Users/yourName/resources/utilities.zip;/Users/yourName/Projects/   App


5.7.2. How to set the CLASSPATH environment variable

The command is the following on Linux and Mac systems:

set   CLASSPATH=path1:path2:path3

On Windows systems, the command is:

set   CLASSPATH=path1;path2;path3

There must not be any space around the equals (=) sign for the command to work.


You are here :  JavaPerspective.com  >   Beginner Tutorials  >   5. Java in Practice  >   5.7. Classpath
Next tutorial :  JavaPerspective.com  >   Beginner Tutorials  >   5. Java in Practice  >   5.8. Java code exercises

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7