'javac' - The Java Compiler
Part:
1
2
3
4
5
6
(Continued from previous part...)
"-classpath" - Specifying Class Path
"-classpath" is an option to specify a list of path names where the compiler will search for compiled type definitions.
If "-classpath" is not specified, the current directory will be used as the class path.
When compiler encounters an unknown type in the source file, it will try to find the type definition by searching
class files in the class path.
To experiment how the compiler uses "-classpath", I wrote the following simple source file, EchoerTest.java:
/**
* EchoerTest.java
* Copyright (c) 2006 by Dr. Herong Yang, http://www.herongyang.com/
*/
public class EchoerTest {
public static void main(String[] a) {
Echoer e = new Echoer();
e.setReq("Hello world!");
System.out.println(e.getRes());
}
}
When I tried to compile this source file, I got the following error:
>javac EchoerTest.jav
EchoerTest.java:7: cannot resolve symbol
symbol : class Echoer
location: class EchoerTest
Echoer e = new Echoer();
As you can see, the compiler failed to find the definition for the type "Echoer".
In order to help the compiler to find the missing type definition, I wrote the following source
code for the Echoer class, Echoer.java:
/**
* Echoer.java
* Copyright (c) 2006 by Dr. Herong Yang, http://www.herongyang.com/
*/
public class Echoer {
private String req = null;
private String res = null;
public void setReq(String r) {
req = new String(r);
char[] a = r.toCharArray();
int n = a.length;
for (int i=0; i<n/2; i++) {
char t = a[i];
a[i] = a[n-1-i];
a[n-i-1] = t;
}
res = new String(a);
}
public String getRes() {
return res;
}
}
Then I compiled the Echoer.java and provided Echoer.class to the class path to compile EchoerTest.java:
>javac Echoer.java
>javac -verbose -classpath . 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.class]
[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]
[total 221ms]
>java EchoerTest
!dlrow olleH
Note that:
- I used "-classpath ." to specify the current directory as the class path for the compiler
to search for the class definition of "Echoer".
- The compiler loaded .\Echoer.class correctly, when Echoer definition was needed.
- The compiler loaded other class definitions from the "rt.jar" from the system path.
(Continued on next part...)
Part:
1
2
3
4
5
6
|