JDK Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.32, 2006

Unnamed Packages

Part:   1  2  3  4  

JDK Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Internationalization

Character Set and Encoding

Socket Communication

Document Object Model (DOM)

XSD Validation in Java

XSL - Transformer in Java

JCA - Private and Public Key Pairs

JCE - Secret Key

SSL (Secure Socket Layer)

SSL - Client Authentication

... Table of Contents

(Continued from previous part...)

To fix the last compilation error, just declare the Hello class as "public". Now let's modify the classes and run the following commands:

C:\hy\tmp>type Hello.java
public class Hello {
   public static void main(String[] a) {
      System.out.println("Hello world!"); 	
   }
}

C:\hy\tmp>del *.class
Could Not Find C:\hy\tmp\*.class

C:\hy\tmp>del com\*.class
Could Not Find C:\hy\tmp\com\*.class

C:\jdk1.3.1\bin\javac -classpath . com\ImportingHelloCom.java

C:\jdk1.3.1\bin\java -classpath . com.ImportingHelloCom
Calling the imported Hello.main()...
Hello world!

As you can see, importing a class defined in an unnamed package into a class defined in a named package is easy with JDK 1.3.1.

Importing Classes Defined in Unnamed Packages to Named Packages Using JDK 1.4.1

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:

  • If you have the source code of the class to be imported from the unnamed package, you can modify the class to put it in a named package.
  • If you don't have the source code of the class to be imported from the unnamed package, you can create a new class in a named package to wrapper the to-be-imported class and compile it with JDK 1.3.1. The wrapper class can then be easily imported into any class with JDK 1.4.1.

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 classs...");
      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 classs...
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!

Conclusion

  • The JDK compiler will compile the imported classes if not compiled yet and source codes are available in the class path.
  • There is really only one unnamed package. All classes defined without a package name are considered to be in this single unnamed package.
  • With JDK 1.3.1 and lower versions, import statement is allowed on classes defined in the unnamed package.
  • With JDK 1.4.1, import statement is not allowed on classes defined in the unnamed package.
  • If you have an old class defined in the unnamed package which can not be directly imported to any classes in named packages with JDK 1.4.1, and you don't have the source code for that old class, you can create a wrapper class in a named package, and compile it with JDK 1.3.1. The wrapper class can then be imported to any classes with JDK 1.4.1.

Part:   1  2  3  4  

Dr. Herong Yang, updated in 2006
JDK Tutorials - Herong's Tutorial Notes - Unnamed Packages