Java Tool Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 5.00

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 EchoerTest.java]
[parsing completed 30ms]
[loading \j2sdk1.5.0\jre\lib\rt.jar(java/lang/Object.class)]
[loading \j2sdk1.5.0\jre\lib\rt.jar(java/lang/String.class)]
[checking EchoerTest]
[loading .\Echoer.java]
[parsing started .\Echoer.java]
[parsing completed 0ms]
[loading \j2sdk1.5.0\jre\lib\rt.jar(java/lang/System.class)]
[loading \j2sdk1.5.0\jre\lib\rt.jar(java/io/PrintStream.class)]
[loading \j2sdk1.5.0\jre\lib\rt.jar(java/io/FilterOutputStream.class)]
[loading \j2sdk1.5.0\jre\lib\rt.jar(java/io/OutputStream.class)]
[wrote EchoerTest.class]
[checking Echoer]
[loading \j2sdk1.5.0\jre\lib\rt.jar(java/lang/StringBuffer.class)]
[wrote .\Echoer.class]
[total 230ms]

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.

Sections in This Chapter

'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

Dr. Herong Yang, updated in 2008
Option '-sourcepath' - Specifying Source Path