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. 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, 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:

C:\hy\tmp>type 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) 2004 by Dr. Herong Yang
 */
package wrapper;
import Hello;
public class HelloWrapper {
   public static void main(String[] a) {
      System.out.println("Wrapping Hello class...");
      Hello.main(a);
   }
}

C:\hy\tmp>type com\ImportingHelloWrapperCom.java
/**
 * ImportingHelloWrapperCom.java
 * Copyright (c) 2004 by Dr. Herong Yang
 */
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);
   }
}

C:\jdk1.3.1\bin\javac -classpath . wrapper\HelloWrapper.java

C:\j2sdk1.4.1\bin\javac -classpath . com\ImportingHelloWrapperCom.java

C:\j2sdk1.4.1\bin\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, don't delete JDK 1.3.1, you might need it one day!

Last update: 2005.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 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

 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 - Secret Key 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

 PDF Printing Version