Option '-classpath' - Specifying Class Path

This section provides a tutorial example on how to use the '-classpath' option to specify the class path for the 'javac' tool to load any classes required during the compilation.

The most commonly used "javac" option is "-classpath", which specifies a list of path names where the compiler will search for compiled type definitions. If "-classpath" is not specified, the current directory will be used as the class path.

When compiler encounters an unknown type in the source file, it will try to find the type definition by searching class files in the class path.

To experiment how the compiler uses "-classpath", I wrote the following simple source file, EchoerTest.java:

/* EchoerTest.java
#- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
public class EchoerTest {
   public static void main(String[] a) {
      Echoer e = new Echoer();
      e.setReq("Hello world!");

When I tried to compile this source file, I got the following error:

C:\herong>javac EchoerTest.java

EchoerTest.java:7: cannot resolve symbol
symbol  : class Echoer
location: class EchoerTest
      Echoer e = new Echoer();

As you can see, the compiler failed to find the definition for the type "Echoer".

In order to help the compiler to find the missing type definition, I wrote the following source code for the Echoer class, Echoer.java:

/* Echoer.java
#- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
public class Echoer {
   private String req = null;
   private String res = null;
   public void setReq(String r) {
      req = new String(r);
      char[] a = r.toCharArray();
      int n = a.length;
      for (int i=0; i<n/2; i++) {
         char t = a[i];
         a[i] = a[n-1-i];
         a[n-i-1] = t;
      res = new String(a);
   public String getRes() {
      return res;

Then I compiled the Echoer.java and provided Echoer.class to the class path to compile EchoerTest.java:

C:\herong>javac Echoer.java

C:\herong>javac -verbose -classpath . EchoerTest.java
[parsing started RegularFileObject[EchoerTest.java]]
[parsing completed 47ms]
[search path for source files: .]
[search path for class files: 
[loading ZipFileIndexFileObject[
[loading ZipFileIndexFileObject[
[checking EchoerTest]
[loading ZipFileIndexFileObject[...java/lang/AutoCloseable.class)]]
[loading RegularFileObject[.\Echoer.class]]
[loading ZipFileIndexFileObject[...java/lang/Byte.class)]]
[loading ZipFileIndexFileObject[...java/lang/Character.class)]]
[loading ZipFileIndexFileObject[...java/lang/Short.class)]]
[loading ZipFileIndexFileObject[...java/lang/Long.class)]]
[loading ZipFileIndexFileObject[...java/lang/Float.class)]]
[loading ZipFileIndexFileObject[...java/lang/Integer.class)]]
[loading ZipFileIndexFileObject[...java/lang/Double.class)]]
[loading ZipFileIndexFileObject[...java/lang/Boolean.class)]]
[loading ZipFileIndexFileObject[...java/lang/Void.class)]]
[loading ZipFileIndexFileObject[...java/lang/System.class)]]
[loading ZipFileIndexFileObject[...java/io/PrintStream.class)]]
[loading ZipFileIndexFileObject[...java/io/FilterOutputStream.class)]]
[loading ZipFileIndexFileObject[...java/io/OutputStream.class)]]
[loading ZipFileIndexFileObject[...java/lang/Appendable.class)]]
[loading ZipFileIndexFileObject[...java/io/Closeable.class)]]
[loading ZipFileIndexFileObject[...java/io/Flushable.class)]]
[wrote RegularFileObject[EchoerTest.class]]
[total 434ms]

C:\herong>java EchoerTest
!dlrow olleH

Note that:

Last update: 2015.

Table of Contents

 About This Book

 Java Tools Terminology

 Installing Java 8 on Windows

'javac' - The Java Program Compiler

 'javac' - Java Compilation Command and Options

 Compiling Hello.java - My First Java Program

Option '-classpath' - Specifying Class Path

 Option '-sourcepath' - Specifying Source Path

 Option '-d' - Specifying Output Directory

 Two Types of 'import' Statements

 'import' Statements Processed by 'javac'

 Option "-g" - Controlling Debugging Information

 'java' - The Java Program Launcher

 'jdb' - The Java Debugger

 'jconsole' - Java Monitoring and Management Console

 'jstat' - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jvisualvm (Java VisualVM) - JVM Visual Tool

 'jar' - The JAR File Tool

 'javap' - The Java Class File Disassembler

 'keytool' - Public Key Certificate Tool

 'native2ascii' - Native-to-ASCII Encoding Converter

 Outdated Tutorials


 PDF Printing Version