Java Tools Tutorials - Herong's Tutorial Notes - Version 5.30, by Dr. Herong Yang

Option '-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.

Let's use the same source files, Echoer.java and EchoerTest.java, to test this:

C:\herong>del Echoer.class
C:\herong>del EchoerTest.class

C:\herong>javac -verbose -sourcepath . EchoerTest.java
[parsing started RegularFileObject[EchoerTest.java]]
[parsing completed 89ms]
[search path for source files: .]
[search path for class files: 
   C:\progra~1\java\jdk1.8.0\jre\lib\resources.jar,
   C:\progra~1\java\jdk1.8.0\jre\lib\rt.jar,
   ...
[loading ZipFileIndexFileObject[
   C:\progra~1\java\jdk1.8.0\lib\ct.sym
   (META-INF/sym/rt.jar/java/lang/Object.class)]]
[loading ZipFileIndexFileObject[
   C:\progra~1\java\jdk1.8.0\lib\ct.sym
   (META-INF/sym/rt.jar/java/lang/String.class)]]
[checking EchoerTest]
[loading ZipFileIndexFileObject[...java/lang/AutoCloseable.class)]]
[loading RegularFileObject[.\Echoer.java]]
[parsing started RegularFileObject[.\Echoer.java]]
[parsing completed 5ms]
[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]]
[checking Echoer]
[loading ZipFileIndexFileObject[...java/lang/StringBuilder.class)]]
[loading ZipFileIndexFileObject[...java/lang/StringBuffer.class)]]
[loading ZipFileIndexFileObject[...java/io/Serializable.class)]]
[loading ZipFileIndexFileObject[...java/lang/Comparable.class)]]
[loading ZipFileIndexFileObject[...java/lang/CharSequence.class)]]
[wrote RegularFileObject[.\Echoer.class]]
[total 660ms]

C:\herong>java EchoerTest
!dlrow olleH

Note that:

  • I used "-sourcepath ." to specify the current directory as the source path for the compiler to search for the source definition of "Echoer".
  • The compiler loaded .\Echoer.java correctly, when Echoer definition was needed.
  • The compiler finished EchoerTest compilation first, then continued to compile Echoer.

Last update: 2014.

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

 References

 PDF Printing Version

Option '-sourcepath' - Specifying Source Path - Updated in 2014, by Dr. Herong Yang