JDK (Java Development Kit) Tutorials
Dr. Herong Yang, Version 5.00

One Class in an Unnamed Package - Hello.java

This section provides a tutorial example on how to place one Java class in an unnamed package and how to compile and run the class in an unnamed package.

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.

In the next test, let's move the current directory to a directory different than the Hello.java directory, and run the following commands:

C:\hy\src>mkdir ..\tmp

C:\hy\src>cd ..\tmp

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

C:\hy\tmp>\jdk1.3.1\bin\javac -classpath . Hello.java
error: cannot read: Hello.java
1 error

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

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

C:\hy\tmp>del ..\src\Hello.class

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

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

C:\hy\tmp>del ..\src\Hello.class

The second test shows us that:

  • Class to be compiled must be specified as command argument with full pathname. Class paths are not searched for the classes to be compiled.

Sections in This Chapter

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

Dr. Herong Yang, updated in 2008
One Class in an Unnamed Package - Hello.java