Importing Classes from Unnamed to Named Packages

This section provides a tutorial example on how to import classes defined in an unnamed package to a named package. The imported class must be defined as 'public'.

Recently, I received an email from a reader asking me how to refer to a class defined in an unnamed package if the calling class is in a named class with JDK 1.4.1 or higher. To answer this question, let's enter a new class called "ImportingHelloCom" in a package called "com", and run the following commands:

\herong\tmp> more com\ImportingHelloCom.java
/* ImportingHelloCom.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
package com;
import Hello;
public class ImportingHelloCom {
   public static void main(String[] a) {
      System.out.println("Calling the imported Hello.main()...");
      Hello.main(a);
   }
}

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

\herong\tmp> del *.class
Could Not Find \herong\tmp\*.class

\herong\tmp> del com\*.class
Could Not Find \herong\tmp\com\*.class

\herong\tmp> \jdk1.3.1_05\bin\javac com\ImportingHelloCom.java
com\ImportingHelloCom.java:5: cannot resolve symbol
symbol: class Hello
import Hello;
^
com\ImportingHelloCom.java:9: cannot resolve symbol
symbol  : variable Hello
location: class ImportingHelloCom
      Hello.main(a);
      ^
2 errors

\herong\tmp> \jdk1.3.1_05\bin\javac -classpath .
   com\ImportingHelloCom.java
com\ImportingHelloCom.java:6: Hello is not public in empty package;
cannot be accessed from outside package
import Hello;
^
com\ImportingHelloCom.java:10: Hello is not public in empty package;
cannot be accessed from outside package
      Hello.main(a);
      ^
com\ImportingHelloCom.java:10: main(java.lang.String[]) in Hello is
not defined in a public class or interface; cannot be accessed from
outside package
      Hello.main(a);
           ^
3 errors

This test shows us that:

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

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

\herong\tmp> del *.class
Could Not Find \herong\tmp\*.class

\herong\tmp> del com\*.class
Could Not Find \herong\tmp\com\*.class

\herong\tmp> \jdk1.3.1_05\bin\javac -classpath .
   com\ImportingHelloCom.java

\herong\tmp> \jdk1.3.1_05\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.

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