Importing Classes from Unnamed to Named Packages - JDK 1.4.1

This section provides a tutorial example on how to import classes defined in an unnamed package to a named package with JDK 1.4.1 or higher. A wrapper class is needed to integrate the class in an unnamed package into a named package with JDK 1.3.1 first.

As we learned earlier in this chapter, if we re-run the previous test with JDK 1.4.1 or higher, we will get a compilation error on the "import Hello" statement.

In this case, if you try my earlier suggestion of removing the import statement, you will get another compilation error about not able to resolve the class name "Hello", because it is not defined in the same package as the calling class.

How to resolve this problem? I am suggesting two options:

To approve my second option, let's continue with our previous tests, enter a wrapper class called HelloWrapper in a package called wrapper, then run the following commands:

\herong\tmp> more wrapper\HelloWrapper.java
/* HelloWrapper.java
 * Wrapping Hello class from the unnamed package into a named package
 * To be compiled with JDK 1.3.1.
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
package wrapper;
import Hello;
public class HelloWrapper {
   public static void main(String[] a) {
      System.out.println("Wrapping Hello class...");
      Hello.main(a);
   }
}

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

\herong\tmp> \jdk1.3.1_05\bin\javac -classpath . wrapper\HelloWrapper.java

\herong\tmp> javac -classpath . com\ImportingHelloWrapperCom.java

\herong\tmp> java -classpath . com.ImportingHelloWrapperCom
Calling the imported Hello.main()...
Wrapping Hello class...
Hello world!

As you can see from the output, the problem is perfectly resolved by using both of versions of JDK together. So after you installed JDK 1.4.1 or higher, don't delete JDK 1.3.1, you might need it one day!

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