"java -classpath" - Specifying Class Path

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

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

To experiment how the Java program launcher uses "-classpath", I wrote the following simple source file, 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;
   }
}

I also wrote the following testing class, 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());
   }
}

Then I stored them in two different directories, and tried to launch EchoerTest:

C:\herong>mkdir .\echo
C:\herong>copy Echoer.java .\echo

C:\herong>mkdir .\test
C:\herong>copy EchoerTest.java .\test

C:\herong>cd test

C:\herong\test>javac -classpath ..\echo EchoerTest.java

C:\herong\test>java EchoerTest
Exception in thread "main" java.lang.NoClassDefFoundError: Echoer
        at EchoerTest.main(EchoerTest.java:6)
Caused by: java.lang.ClassNotFoundException: Echoer
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass...
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader....
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java...
        ... 1 more

C:\herong\test>java -classpath ..\echo EchoerTest
Error: Could not find or load main class EchoerTest
Caused by: java.lang.ClassNotFoundException: EchoerTest

C:\herong\test>java -classpath .;..\echo EchoerTest
!dlrow olleH        

Note that:

Last update: 2018.

Table of Contents

 About This Book

 Java Tools Terminology

 Java Tools Included in JDK

 javac - The Java Program Compiler

 jar - The JAR File Tool

 jlink - The JRE Linker

 jmod - The JMOD File Tool

 jimage - The JIMAGE File Tool

java - The Java Program Launcher

 java - Program Launching Command and Options

 Launching Hello.java - My First Java Program

"java -classpath" - Specifying Class Path

 "java -jar" - Specifying Executable JAR File

 "java -X" - Specifying Non-Standard Options

 "java --list-modules" - Listing Modules in JDK

 "java --describe-module" - Printing Module Definition

 "java --module" - Launching Program from Module

 "java --module" - Launching Program from Module JAR

 javaw - Launching Java Programs without Console

 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