'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 described in the previous section, I did the following:

C:\herong>mkdir .\com
C:\herong>mkdir .\com\herong
C:\herong>mkdir .\com\herong\util

C:\herong>copy ClsA.java .\com\herong\util
C:\herong>copy ClsB.java .\com\herong\util

C:\herong>javac ImportTestA.java
ImportTestA.java:8: 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.
1 error

What's wrong with ImportTestA.java, which uses the "On-demand type import" statement? The answer is obvious if we use the -verbose compiler option:

C:\herong>javac -verbose ImportTestA.java
[parsing started RegularFileObject[ImportTestA.java]]
[parsing completed 23ms]
[search path for source files: .]
[search path for class files: C:\progra~1\java\jdk1.8.0\jre\lib\...
[loading ZipFileIndexFileObject[C:\progra~1\java\jdk1.8.0\lib\ct...
[loading ZipFileIndexFileObject[C:\progra~1\java\jdk1.8.0\lib\ct...
[checking ImportTestA]
[loading ZipFileIndexFileObject[C:\progra~1\java\jdk1.8.0\lib\ct...
[loading RegularFileObject[.\ClsA.java]]
[parsing started RegularFileObject[.\ClsA.java]]
[parsing completed 1ms]
ImportTestA.java:8: 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.
[total 155ms]
1 error

C:\herong>javac -verbose ImportTestB.java
[parsing started RegularFileObject[ImportTestB.java]]
[parsing completed 24ms]
[search path for source files: .]
[search path for class files: C:\progra~1\java\jdk1.8.0\jre\lib\...
[loading RegularFileObject[.\com\herong\util\ClsA.java]]
[parsing started RegularFileObject[.\com\herong\util\ClsA.java]]
[parsing completed 0ms]
[loading RegularFileObject[.\com\herong\util\ClsB.java]]
[parsing started RegularFileObject[.\com\herong\util\ClsB.java]]
[parsing completed 0ms]
[loading ZipFileIndexFileObject[C:\progra~1\java\jdk1.8.0\lib\ct...
[loading ZipFileIndexFileObject[C:\progra~1\java\jdk1.8.0\lib\ct...
[checking ImportTestB]
[loading ZipFileIndexFileObject[C:\progra~1\java\jdk1.8.0\lib\ct...
[wrote RegularFileObject[ImportTestB.class]]
[checking com.herong.util.ClsA]
[loading ZipFileIndexFileObject[C:\progra~1\java\jdk1.8.0\lib\ct...
[loading ZipFileIndexFileObject[C:\progra~1\java\jdk1.8.0\lib\ct...
[loading ZipFileIndexFileObject[C:\progra~1\java\jdk1.8.0\lib\ct...
[loading ZipFileIndexFileObject[C:\progra~1\java\jdk1.8.0\lib\ct...
[loading ZipFileIndexFileObject[C:\progra~1\java\jdk1.8.0\lib\ct...
[loading ZipFileIndexFileObject[C:\progra~1\java\jdk1.8.0\lib\ct...
[loading ZipFileIndexFileObject[C:\progra~1\java\jdk1.8.0\lib\ct...
[loading ZipFileIndexFileObject[C:\progra~1\java\jdk1.8.0\lib\ct...
[loading ZipFileIndexFileObject[C:\progra~1\java\jdk1.8.0\lib\ct...
[loading ZipFileIndexFileObject[C:\progra~1\java\jdk1.8.0\lib\ct...
[loading ZipFileIndexFileObject[C:\progra~1\java\jdk1.8.0\lib\ct...
[loading ZipFileIndexFileObject[C:\progra~1\java\jdk1.8.0\lib\ct...
[loading ZipFileIndexFileObject[C:\progra~1\java\jdk1.8.0\lib\ct...
[loading ZipFileIndexFileObject[C:\progra~1\java\jdk1.8.0\lib\ct...
[loading ZipFileIndexFileObject[C:\progra~1\java\jdk1.8.0\lib\ct...
[loading ZipFileIndexFileObject[C:\progra~1\java\jdk1.8.0\lib\ct...
[wrote RegularFileObject[.\com\herong\util\ClsA.class]]
[checking com.herong.util.ClsB]
[wrote RegularFileObject[.\com\herong\util\ClsB.class]]
[total 209ms]

What happened with ImportTestA.java compilation was that:

The compilation of ImportTestB.java went very smoothly:

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.herong.util;".

This test shows us that:

Last update: 2015.

Table of Contents

 About This Book

 Java Tools Terminology

 Installing Java 8 on Windows

'javac' - The Java Program Compiler

 'javac' - Java Compilation Command and Options

 Compiling Hello.java - My First Java Program

 Option '-classpath' - Specifying Class Path

 Option '-sourcepath' - Specifying Source Path

 Option '-d' - Specifying Output Directory

 Two Types of 'import' Statements

'import' Statements Processed by 'javac'

 Option "-g" - Controlling Debugging Information

 'java' - The Java Program Launcher

 'jdb' - The Java Debugger

 'jconsole' - Java Monitoring and Management Console

 'jstat' - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jvisualvm (Java VisualVM) - JVM Visual Tool

 'jar' - The JAR File Tool

 'javap' - The Java Class File Disassembler

 'keytool' - Public Key Certificate Tool

 'native2ascii' - Native-to-ASCII Encoding Converter

 Outdated Tutorials

 References

 PDF Printing Version