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

(Continued from previous part...)

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.

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

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:

  • The compiler is smart. It automatically compiles the source code of the referred class, Hello, if Hello.class can not be found in the class paths.

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:

  • The compiler is indeed smart. It will search for source code in class paths, even the referred class is not in the same directory as the class that is being compiled.

(Continued on next part...)

Part:   1  2   3  4 

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