"import" Statements Processed by "javac"

This section provides a tutorial example on how the 'javac' tool process two types of 'import' statements differently when loading required class types.

After saving all 4 source files in the current directory as described in the previous section, I intentionally created copies of ClsA.java and ClsB.java in ".\com\herongyang\util" sub-directory.

herong> mkdir .\com
herong> mkdir .\com\herong
herong> mkdir .\com\herongyang\util

herong> copy ClsA.java .\com\herongyang\util
herong> copy ClsB.java .\com\herongyang\util

herong> dir Cls*.java
   216 ClsA.java
   216 ClsB.java

herong> dir .\com\herongyang\util\Cls*.java
   216 ClsA.java
   216 ClsB.java

Then I tried to compile ImportTestA.java that uses the "On-demand type import" statement of "import com.herongyang.util.*;":

herong> javac ImportTestA.java

ImportTestA.java:7: error: cannot access ClsA
      ClsA a = new ClsA();
      ^
  bad source file: .\ClsA.java
    file does not contain class ClsA
    Please remove or make sure it appears in the correct subdirectory
    of the sourcepath.
ImportTestA.java:9: error: cannot access ClsB
      ClsB b = new ClsB();
      ^
  bad source file: .\ClsB.java
    file does not contain class ClsB
    Please remove or make sure it appears in the correct subdirectory
    of the sourcepath.
2 errors

So what went wrong with the compilation of ImportTestA.java? I found the answer by using the "-verbose" compiler option:

herong> javac -verbose ImportTestA.java

[parsing started SimpleFileObject[C:\herong\ImportTestA.java]]
[parsing completed 31ms]
[loading /modules/jdk.xml.ws/module-info.class]
[loading /modules/jdk.charsets/module-info.class]
[loading /modules/jdk.deploy.controlpanel/module-info.class]
...
[search path for source files: .]
[search path for class files: C:\Progra~1\java\jdk-10.0.1\lib\modules,.]
[loading /modules/java.base/java/lang/Object.class]
[loading /modules/java.base/java/lang/String.class]
[loading /modules/java.base/java/lang/Deprecated.class]
...
[checking ImportTestA]
[loading /modules/java.base/java/io/Serializable.class]
[loading /modules/java.base/java/lang/AutoCloseable.class]

[loading .\ClsA.java]
[parsing started DirectoryFileObject[.:ClsA.java]]
[parsing completed 0ms]
ImportTestA.java:7: error: cannot access ClsA
      ClsA a = new ClsA();
      ^
  bad source file: .\ClsA.java
    file does not contain class ClsA
    Please remove or make sure it appears in the correct subdirectory of
    the sourcepath.

[loading .\ClsB.java]
[parsing started DirectoryFileObject[.:ClsB.java]]
[parsing completed 0ms]
ImportTestA.java:9: error: cannot access ClsB
      ClsB b = new ClsB();
      ^
  bad source file: .\ClsB.java
    file does not contain class ClsB
    Please remove or make sure it appears in the correct subdirectory of
    the sourcepath.

[checking com.herongyang.util.ClsA]
[loading /modules/java.base/java/lang/System.class]
[loading /modules/java.base/java/io/PrintStream.class]
[loading /modules/java.base/java/lang/Appendable.class]
[checking com.herongyang.util.ClsB]
...

What happened with ImportTestA.java compilation was that:

Of course, we know how to fix the problem. Just remove ClsA.java and ClsB.java from the current directory. "javac" will continue to search for the ClsA in the package specified in "import com.herongyang.util;".

However compilation went ok with ImportTestB.java that uses "Single type import" statements of "import com.herongyang.util.ClsA;" and "import com.herongyang.util.ClsB;". Extra copies of "ClsB.java and ClsB.java can stay in the current directory.

herong> javac -verbose ImportTestB.java

[parsing started SimpleFileObject[C:\herong\ImportTestB.java]]
[parsing completed 31ms]
[loading /modules/jdk.xml.ws/module-info.class]
[loading /modules/jdk.charsets/module-info.class]
[loading /modules/jdk.deploy.controlpanel/module-info.class]
...
[search path for source files: .]
[search path for class files: C:\Progra~1\java\jdk-10.0.1\lib\modules,.]

[loading .\com\herongyang\util\ClsA.java]
[parsing started DirectoryFileObject[.:com/herong/util/ClsA.java]]
[parsing completed 0ms]

[loading .\com\herongyang\util\ClsB.java]
[parsing started DirectoryFileObject[.:com/herong/util/ClsB.java]]
[parsing completed 0ms]

[loading /modules/java.base/java/lang/Object.class]
[loading /modules/java.base/java/lang/String.class]
[loading /modules/java.base/java/lang/Deprecated.class]
...
[checking ImportTestB]
[loading /modules/java.base/java/io/Serializable.class]
[loading /modules/java.base/java/lang/AutoCloseable.class]
[wrote SimpleFileObject[C:\herong\ImportTestB.class]]
[checking com.herongyang.util.ClsA]
[loading /modules/java.base/java/lang/System.class]
[loading /modules/java.base/java/io/PrintStream.class]
[loading /modules/java.base/java/lang/Appendable.class]
...
[wrote DirectoryFileObject[.:com/herong/util/ClsA.class]]
[checking com.herongyang.util.ClsB]
[wrote DirectoryFileObject[.:com/herong/util/ClsB.class]]
[total 392ms]

What happened with ImportTestB.java compilation was that:

This test shows us that:

Table of Contents

 About This Book

 Java Tools Terminology

 Java Tools Included in JDK

javac - The Java Program Compiler

 javac - Java Compilation Command and Options

 Compiling Hello.java - My First Java Program

 "javac -classpath" - Specifying Class Path

 "javac -verbose" - Printing Compilation Details

 "javac -sourcepath" - Specifying Source Path

 "javac -d" - Specifying Output Directory

 Two Types of "import" Statements

"import" Statements Processed by "javac"

 "javac -g" - Controlling Debugging Information

 "javac --module" - Compiling Entire Module

 "javac -X" - Specifying Non-Standard Options

 java - The Java Program Launcher

 jar - The JAR File Tool

 jlink - The JRE Linker

 jmod - The JMOD File Tool

 jimage - The JIMAGE File Tool

 jpackage - Binary Package Builder

 javadoc - The Java Document Generator

 jdeps - The Java Class Dependency Analyzer

 jdeprscan - The Java Deprecated API Scanner

 jdb - The Java Debugger

 jcmd - The JVM Diagnostic Tool

 jconsole - Java Monitoring and Management Console

 jstat - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jhsdb - The Java HotSpot Debugger

 jvisualvm (Java VisualVM) - JVM Visual Tool

 jmc - Java Mission Control

 javap - The Java Class File Disassembler

 keytool - Public Key Certificate Tool

 jarsigner - JAR File Signer

 jshell - Java Language Shell

 jrunscript - Script Code Shell

 Miscellaneous Tools

 native2ascii - Native-to-ASCII Encoding Converter

 JAB (Java Access Bridge) for Windows

 Archived Tutorials

 References

 Full Version in PDF/EPUB