Java Tools Tutorials - Herong's Tutorial Notes - Version 5.30, by Dr. Herong Yang

'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:

  • 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 directory 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.java immediately.
  • Then, the compiler reached "import com.herong.util.ClsB;", it loaded .\com\herong\util\ClsB.java immediately.
  • 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.

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:

  • "Single type import" statements load classes immediately.
  • "javac" searches for definitions of new types first in the loaded classes, then in the source path, and finally in the packages specified in the "On-demand type import" statements.

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

'import' Statements Processed by 'javac' - Updated in 2014, by Dr. Herong Yang