for example:
--- package to be referenced
package c05.local;
public class PackagedClass {
public PackagedClass() {
System.out.println("Creating a packaged class.");
}
}
---
--- referencing package
//package c05.foreign; // (1)
import c05.local.*;
public class Foreign {
public static void main(String[] args) {
PackagedClass pc = new PackagedClass();
}
}
---
Now if these are off the classpath in the directories appropriate for the
packages and line (1) is commented out,
"java Foreign" produces output of:
"Creating a packaged class."
However, if line (1) is uncommented, the output is:
Exception in thread "main" java.lang.NoClassDefFoundError: Foreign (wrong
name:
c05/foreign/Foreign)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:486)
at
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:11
1)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:248)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:297)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:286)
at java.lang.ClassLoader.loadClass(ClassLoader.java:253)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:313)
Any ideas? I'm running sun jdk 1.3.0.
Cheer,
Dan Sketcher.
ps. Thanks to Bruce Eckel for a great java book.
--
_________________________________________________
Mr Dan Sketcher
P.O. Box 10114,
Brisbane, Adelaide St,
QLD 4000
AUSTRALIA
email: dansk...@hotmailSPAM.com (remove the SPAM)
Cheer,
Dan Sketcher.
phone: +61 7 3876 8962
email: dansk...@hotmailSPAM.com (remove the SPAM)
mobile: 0416 265 235
It also appears that you've got your classpath set incorrectly. The
directory containing Foreign.class should not be in your classpath. The
directory containing the "c05" directory should be in your classpath. Java
will automatically look in the c05/foreign subdirectory because that's the
package that the class is in.
Chris Smith