Java Tools Tutorials - Herong's Tutorial Examples - v6.23, by Herong Yang
"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) 2005 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) 2005 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:
herong> mkdir .\echo herong> copy Echoer.java .\echo herong> mkdir .\test herong> copy EchoerTest.java .\test herong> cd test herong\test> javac -classpath ..\echo EchoerTest.java 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 herong\test> java -classpath ..\echo EchoerTest Error: Could not find or load main class EchoerTest Caused by: java.lang.ClassNotFoundException: EchoerTest herong\test> java -classpath .;..\echo EchoerTest !dlrow olleH
Note that:
Table of Contents
javac - The Java Program Compiler
►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
jpackage - Binary Package Builder
javadoc - The Java Document Generator
jdeps - The Java Class Dependency Analyzer
jdeprscan - The Java Deprecated API Scanner
jcmd - The JVM Diagnostic Tool
jconsole - Java Monitoring and Management Console
jstat - JVM Statistics Monitoring Tool
jhsdb - The Java HotSpot Debugger
jvisualvm (Java VisualVM) - JVM Visual Tool
javap - The Java Class File Disassembler
keytool - Public Key Certificate Tool
jrunscript - Script Code Shell
native2ascii - Native-to-ASCII Encoding Converter