'javac' - The Java Compiler
Part:
1
2
3
4
5
6
(Continued from previous part...)
4. ImportTestB.java:
/**
* ImportTestB.java
* Copyright (c) 2006 by Dr. Herong Yang, http://www.herongyang.com/
*/
import com.herong.util.ClsA;
import com.herong.util.ClsB;
public class ImportTestB {
public static void main(String[] arg){
ClsA a = new ClsA();
a.printInfo();
ClsB b = new ClsB();
b.printInfo();
}
}
After putting all 4 source files in the current directory, I did the following:
>mkdir .\com
>mkdir .\com\herong
>mkdir .\com\herong\util
>copy ClsA.java .\com\herong\util
>copy ClsB.java .\com\herong\util
>javac ImportTestA.java
ImportTestA.java:8: cannot access ClsA
bad class file: .\ClsA.java
file does not contain class ClsA
Please remove or make sure it appears in the correct subdirectory of
the classpath.
ClsA a = new ClsA();
^
1 error
>javac ImportTestB.java
What's wrong with ImportTestA.java, which uses the "On-demand type import" statement? The answer is obviouse
if we use the -verbose compiler option:
>javac -verbose ImportTestA.java
[parsing started ImportTestA.java]
[parsing completed 30ms]
[loading c:\j2sdk1.4.2\jre\lib\rt.jar(java/lang/Object.class)]
[loading c:\j2sdk1.4.2\jre\lib\rt.jar(java/lang/String.class)]
[checking ImportTestA]
[loading .\ClsA.java]
[parsing started .\ClsA.java]
[parsing completed 0ms]
ImportTestA.java:8: cannot access ClsA
bad class file: .\ClsA.java
file does not contain class ClsA
Please remove or make sure it appears in the correct subdirectory of
the classpath.
ClsA a = new ClsA();
^
[total 230ms]
1 error
>javac -verbose ImportTestB.java
[parsing started ImportTestB.java]
[parsing completed 30ms]
[loading .\com\herong\util\ClsA.class]
[loading .\com\herong\util\ClsB.class]
[loading c:\j2sdk1.4.2\jre\lib\rt.jar(java/lang/Object.class)]
[loading c:\j2sdk1.4.2\jre\lib\rt.jar(java/lang/String.class)]
[checking ImportTestB]
[wrote ImportTestB.class]
[total 221ms]
What happened with ImportTestA.java compilation was that:
- First, the compiler reached "import com.herong.util.*;", but it did not load any type definition.
- Then, the compiler reached "ClsA a = new ClsA();", it started to search for the definition of "ClsA".
- Then, the compiler found "ClsA.java" in the current directory.
- Then, the compiler found out that "ClsA.java" is in the wrong directry path comparing with its package name.
The compilation of ImportTestB.java went very smoothly:
- First, the compiler reached "import com.herong.util.ClsA;", it loaded .\com\herong\util\ClsA.class
immediately. ClsA.java has already compiled by the previous test.
- Then, the compiler reached "import com.herong.util.ClsB;", it loaded .\com\herong\util\ClsB.class
immediately. ClsB.java has already compiled by the previous test.
- Then, the compiler reached "ClsA a = new ClsA();", it had the definition of ClsA ready to use, no search needed.
- Then, the compiler reached "ClsB b = new ClsB();", it had the definition of ClsB ready to use, no search needed.
(Continued on next part...)
Part:
1
2
3
4
5
6
|