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 \herong\src, enter the following commands in a command window:

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

\herong\src> del Hello.class
Could Not Find \herong\src\Hello.class

\herong\src> set CLASSPATH=

\herong\src> javac Hello.java

\herong\src> java Hello
Hello world!

\herong\src> del Hello.class
\herong\src> javac -classpath \herong Hello.java

\herong\src> java -classpath \herong Hello
Exception in thread "main" java.lang.NoClassDefFoundError: Hello

\herong\src> del Hello.class

\herong\src> javac -classpath . Hello.java

\herong\src> java -classpath . Hello
Hello world!

\herong\src> del Hello.class

This test tells us that:

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

\herong\src> mkdir ..\tmp

\herong\src> cd ..\tmp

\herong\tmp> del ..\src\Hello.class
Could Not Find \herong\src\Hello.class

\herong\tmp> javac -classpath . Hello.java
error: cannot read: Hello.java
1 error

\herong\tmp> javac -classpath . ..\src\Hello.java

\herong\tmp> java -classpath . Hello
Exception in thread "main" java.lang.NoClassDefFoundError: Hello

\herong\tmp> del ..\src\Hello.class

\herong\tmp> javac -classpath ..\src ..\src\Hello.java

\herong\tmp> java -classpath ..\src Hello
Hello world!

\herong\tmp> del ..\src\Hello.class

The second test shows us that:

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