"javac -sourcepath" - Specifying Source Path

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

If you use a new type, and you don't have the class definition of that type, but you have its source definition, you can use the "-sourcepath sourcepath" option to tell compiler to get that source definition. The compiler will compile that source definition into class definition.

I used the same source files, Echoer.java and EchoerTest.java, to test the compiler "-sourcepath" option:

herong> del Echoer.class
herong> del EchoerTest.class

herong> javac -verbose -sourcepath . EchoerTest.java

[parsing started SimpleFileObject[C:\herong\EchoerTest.java]]

[parsing completed 15ms]
[loading /modules/javafx.fxml/module-info.class]
[loading /modules/java.activation/module-info.class]
[loading /modules/jdk.zipfs/module-info.class]
...

[search path for source files: .]
[search path for class files: C:\Progra~1\java\jdk-12.0.1\lib\modules,.]
[loading /modules/java.base/java/lang/Object.class]
[loading /modules/java.base/java/lang/String.class]
[loading /modules/java.base/java/lang/Deprecated.class]
...
[checking EchoerTest]
[loading /modules/java.base/java/io/Serializable.class]
[loading /modules/java.base/java/lang/AutoCloseable.class]

[loading .\Echoer.java]
[parsing started DirectoryFileObject[.:Echoer.java]]
[parsing completed 15ms]

[loading /modules/java.base/java/lang/System.class]
[loading /modules/java.base/java/io/PrintStream.class]
[loading /modules/java.base/java/lang/Appendable.class]
[loading /modules/java.base/java/io/Closeable.class]
,,,
[wrote SimpleFileObject[C:\herong\EchoerTest.class]]

[checking Echoer]
[loading /modules/java.base/java/lang/StringBuilder.class]
[loading /modules/java.base/java/lang/StringBuffer.class]

[wrote DirectoryFileObject[.:Echoer.class]]
[total 360ms]

herong> java EchoerTest
!dlrow olleH

Note that:

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

 java - The Java Program Launcher

 jar - The JAR File Tool

 jlink - The JRE Linker

 jmod - The JMOD File Tool

 jimage - The JIMAGE File Tool

 jpackage - Binary Package Builder

 javadoc - The Java Document Generator

 jdeps - The Java Class Dependency Analyzer

 jdeprscan - The Java Deprecated API Scanner

 jdb - The Java Debugger

 jcmd - The JVM Diagnostic Tool

 jconsole - Java Monitoring and Management Console

 jstat - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jhsdb - The Java HotSpot Debugger

 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

 jrunscript - Script Code Shell

 Miscellaneous Tools

 native2ascii - Native-to-ASCII Encoding Converter

 JAB (Java Access Bridge) for Windows

 Archived Tutorials

 References

 Full Version in PDF/EPUB