"javac -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!");
      System.out.println(e.getRes());
   }
}

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

C:\herong>javac EchoerTest.java

EchoerTest.java:6: error: cannot find symbol
      Echoer e = new Echoer();
      ^
  symbol:   class Echoer
  location: class EchoerTest
EchoerTest.java:6: error: cannot find symbol
      Echoer e = new Echoer();
                     ^
  symbol:   class Echoer
  location: class EchoerTest
2 errors

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>dir Echoer*.class
    657 Echoer.class

C:\herong>javac -classpath . EchoerTest.java
    657 Echoer.class
    528 EchoerTest.class

The compilation was successful. Now I can run the EchoerTest class with "java" command.

C:\herong>java -classpath . EchoerTest

!dlrow olleH

Note that the "-classpath ." option tells the "javac" command to search for the definition for any unknown "symbol" in the given path. In this case, the unknown symbol, "Echoer" is a class name. The definition is given in the ".\Echoer.class" file.

Last update: 2018.

Table of Contents

 About This Book

 Java Tools Terminology

 Java Tools Included in JDK

javac - The Java Program Compiler

 javac - Java Compilation Command and Options

 Compiling Hello.java - My First Java Program

"javac -classpath" - Specifying Class Path

 "javac -verbose" - Printing Compilation Details

 "javac -sourcepath" - Specifying Source Path

 "javac -d" - Specifying Output Directory

 Two Types of "import" Statements

 "import" Statements Processed by "javac"

 "javac -g" - Controlling Debugging Information

 "javac --module" - Compiling Entire Module

 "javac -X" - Specifying Non-Standard Options

 jar - The JAR File Tool

 jlink - The JRE Linker

 jmod - The JMOD File Tool

 jimage - The JIMAGE File Tool

 java - The Java Program Launcher

 jdb - The Java Debugger

 jcmd - The JVM Diagnostic Tool

 jconsole - Java Monitoring and Management Console

 jstat - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jvisualvm (Java VisualVM) - JVM Visual Tool

 jmc - Java Mission Control

 javap - The Java Class File Disassembler

 keytool - Public Key Certificate Tool

 jarsigner - JAR File Signer

 jshell - Java Language Shell

 native2ascii - Native-to-ASCII Encoding Converter

 Outdated Tutorials

 References

 Full Version in PDF/EPUB