|
'javac' - The Java Compiler
Part:
1
2
3
4
5
6
(Continued from previous part...)
Test 2 - Storing PackagedHello.java in the right directory:
>mkdir .\com
>mkdir .\com\herong
>mkdir .\com\herong\util
>copy PackagedHello.java .\com\herong\util
>del PackagedHell.*
>javac .\com\herong\util\PackagedHello.java
>dir .\com\herong\util
459 PackagedHello.class
264 PackagedHello.java
>java -cp . com.herong.util.PackagedHello
Packaged: Hello world!
As you can see, the compiler outputs the class file in the same directory as the source file.
Test 3 - Outputing PackagedHello.class to a new directory:
>mkdir .\cls
>javac -d .\cls .\com\herong\util\PackagedHello.java
>dir .\cls\com\herong\util
459 PackagedHello.class
>java -cp .\cls com.herong.util.PackagedHello
Packaged: Hello world!
This time, the compiler outputs the class file under a directory path: .\cls.
"import" Statements
Java offers two types of "import" statements:
1. Single Type Import: "import TypeName;" - It loads the definition of a single type immediately from
the specified package.
2. On-Demand Type Import: "import PackageName.*;" - It tells JVM to search this package for any missing type.
"javac" command processes there two different types of "import" statements differently. I wrote the following
4 source files.
1. ClsA.java:
/**
* ClsA.java
* Copyright (c) 2006 by Dr. Herong Yang, http://www.herongyang.com/
*/
package com.herong.util;
public class ClsA {
public void printInfo() {
System.out.println("Class: com.herong.util.ClsA");
}
}
2. ClsB.java:
/**
* ClsB.java
* Copyright (c) 2006 by Dr. Herong Yang, http://www.herongyang.com/
*/
package com.herong.util;
public class ClsB {
public void printInfo() {
System.out.println("Class: com.herong.util.ClsB");
}
}
3. ImportTestA.java:
/**
* ImportTestA.java
* Copyright (c) 2006 by Dr. Herong Yang, http://www.herongyang.com/
*/
import com.herong.util.*;
public class ImportTestA {
public static void main(String[] arg){
ClsA a = new ClsA();
a.printInfo();
ClsB b = new ClsB();
b.printInfo();
}
}
(Continued on next part...)
Part:
1
2
3
4
5
6
|