Importing Classes Defined in Unnamed Packages

This section provides a tutorial example on how to import classes defined in an unnamed package - done in the same as importing classed defined in named packages with JDK 1.3.1. But it is not allowed with JDK 1.4.1.

For classes defined in unnamed packages, their simple names are identical to their fully qualified names. Therefore, there is no need to use import statement in the compilation units where they are referred.

But if you really want to use an import statement, it shouldn't be a problem. So let's enter another class, ImportingHello.java, in \herong\tmp directory, and run the following commands to see what happens with the import statement:

\herong\tmp> more ImportingHello.java
/* ImportingHello.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
import Hello;
public class ImportingHello {
   public static void main(String[] a) {
      System.out.println("Calling the imported Hello.main()...");
      Hello.main(a);
   }
}

When I tried to compile this program with JDK 1.4 or higher, I got a compilation error:

\herong\tmp> javac -classpath ..\src ImportingHello.java
ImportingHello.java:5: '.' expected
import Hello;
            ^
1 error

So importing a class from a unnamed package is not supported.

But this was supported back in JDK 1.3:

\herong\tmp> del *.class
Could Not Find \herong\tmp\*.class

\herong\tmp> del ..\src\*.class
Could Not Find \herong\src\*.class

\herong\tmp> \jdk1.3.1_05\bin\javac -classpath ..\src ImportingHello.java

\herong\tmp> \jdk1.3.1_05\bin\java -classpath .;..\src ImportingHello
Calling the imported Hello.main()...
Hello world!

\herong\tmp> del *.class

\herong\tmp> del ..\src\*.class

Table of Contents

 About This JDK Tutorial Book

 JDK (Java Development Kit)

 Java Date-Time API

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

Calling and Importing Classes Defined in Unnamed Packages

 What Is an Unnamed Package?

 One Class in an Unnamed Package - Hello.java

 Two Classes in Unnamed Packages - Hello.java and CallingHello.java

Importing Classes Defined in Unnamed Packages

 Importing Classes from Unnamed to Named Packages

 Importing Classes from Unnamed to Named Packages - JDK 1.4.1

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Java Logging

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials

 References

 Full Version in PDF/EPUB