Two Classes in Unnamed Packages - Hello.java and CallingHello.java

This section provides a tutorial example on how to place two Java classes in an unnamed package and how to call one class from another class in the unnamed package.

Testing with two classes is definitely more interesting. Let's enter another class called, CallingHello.java, in C:\hy\src directory, and run the following commands:

C:\hy\src>type CallingHello.java
/**
 * CallingHello.java
 * Copyright (c) 2003 by Dr. Herong Yang
 */
public class CallingHello {
   public static void main(String[] a) {
      System.out.println("Calling Hello.main()...");
      Hello.main(a);
   }
}

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

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

C:\hy\src>\jdk1.3.1\bin\java -classpath . CallingHello
Calling Hello.main()...
Hello world!

C:\hy\src>del *.class

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

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

C:\hy\tmp>\jdk1.3.1\bin\java -classpath ..\src CallingHello
Calling Hello.main()...
Hello world!

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

This test shows us that:

In the next test, let's separate the classes into two directories, and run the following commands:

C:\hy\tmp>copy ..\src\CallingHello.java

C:\hy\tmp>del ..\src\CallingHello.java

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

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

C:\hy\tmp>\jdk1.3.1\bin\javac -classpath . CallingHello.java
CallingHello.java:8: cannot resolve symbol
symbol  : variable Hello
location: class CallingHello
      Hello.main(a);
      ^
1 error

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

C:\hy\tmp>\jdk1.3.1\bin\java -classpath . CallingHello
Calling Hello.main()...
Exception in thread "main" java.lang.NoClassDefFoundError: Hello
        at CallingHello.main(CallingHello.java:8)

C:\hy\tmp>\jdk1.3.1\bin\java -classpath .;..\src CallingHello
Calling Hello.main()...
Hello world!

C:\hy\tmp>del *.class

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

This test confirms that:

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