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

This chapter explains:

  • What is an unnamed package.
  • How classes in unnamed packages are created, and referred.
  • How to avoid compilation issue with JDK 1.4.1 when using import statement on classes defined in unnamed packages.
  • How to import classes defined in unnamed packages into named packages.

What Is an Unnamed Package

Unnamed packages contain compilation units (classes or interfaces) that have no package declaration statement. For example, the following class is a compilation unit in an unnamed package, Hello.java:

class Hello {
   public static void main(String[] a) {
      System.out.println("Hello world!"); 	
   }
}

There are a number of questions about unnamed packages we need to answer:

  • How many unnamed packages can JDK support?
  • How compilation units in each unnamed package are stored?
  • How to refer to the compilation units in unnamed packages?

Based on the documentation of JDK for Windows system, my guess is that JDK supports multiple unnamed packages. Each unnamed package is stored in the top directory of each class path specified in one of the following three ways:

  • -classpath path;path;...
  • set CLASSPATH=path;path;...
  • "current directory"

Let's do some tests to answer these questions and to confirm my guess.

One Class in an Unnamed Package - Hello.java

I will begin with a simple case, where there is only one class that has no package declaration statement, so it can be stored in any unnamed package. Assuming that we have Hello.java entered in a directory called C:\hy\src, enter the following commands in a command window:

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

C:\hy\src>del Hello.class
Could Not Find C:\herong\src\Hello.class

C:\hy\src>set CLASSPATH=

C:\hy\src>\jdk1.3.1\bin\java -version
java version "1.3.1_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1_01)
Java HotSpot(TM) Client VM (build 1.3.1_01, mixed mode)

C:\hy\src>\jdk1.3.1\bin\javac Hello.java

C:\hy\src>\jdk1.3.1\bin\java Hello
Hello world!

C:\hy\src>del Hello.class
C:\hy\src>\jdk1.3.1\bin\javac -classpath \hy Hello.java

C:\hy\src>\jdk1.3.1\bin\java -classpath \hy Hello
Exception in thread "main" java.lang.NoClassDefFoundError: Hello

C:\hy\src>del Hello.class

C:\hy\src>\jdk1.3.1\bin\javac -classpath . Hello.java

C:\hy\src>\jdk1.3.1\bin\java -classpath . Hello
Hello world!

C:\hy\src>del Hello.class

This test tells us that:

  • I am using JDK 1.3.1_01.
  • If there is no -classpath command option specified, and no CLASSPATH environment variable defined, there is one default class path: current directory.
  • Class paths are searched for the class being invoked in the execution process.

(Continued on next part...)

Part:   1   2  3  4 

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