'javac' - The Java Compiler
Part:
1
2
3
4
5
6
(Continued from previous part...)
"-sourcepath" - Specifying Source Path
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:
>del Echoer.class
>del EchoerTest.class
>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]
>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.
"-d" - Specifying Output Directory
If you are writing a real Java application, you will organize your Java classes into packages.
The Java source files must be stored in a directory with the path names match the package names.
For example, if a source file, Some.java, is defined in a package name as: "com.herong.util", it must be
stored in a directory named as: .\com\herong\util\Some.java.
By default, "javac" will output the class file in the same directory as the source file.
But you could change this default behavior by using the "-d" option. It will make "javac"
to output the class files into the specified directory.
To test this option, I wrote the following Java source file: PackagedHello.java
/**
* PackagedHello.java
* Copyright (c) 2006 by Dr. Herong Yang, http://www.herongyang.com/
*/
package com.herong.util;
public class PackagedHello {
public static void main(String[] a) {
System.out.println("Packaged: Hello world!");
}
}
Test 1 - Storing PackagedHello.java in the wrong directory:
>dir PackagedHell.*
264 PackagedHello.java
>javac PackagedHello.java
>dir PackagedHell.*
459 PackagedHello.class
264 PackagedHello.java
>java -cp . PackagedHello
Exception in thread "main" java.lang.NoClassDefFoundError:
PackagedHello (wrong name: com/herong/util/PackagedHello)
This proves that packaged classes can not be stored in any directory.
(Continued on next part...)
Part:
1
2
3
4
5
6
|