The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
July 2001



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Run it this way

Posted by Amit Dubey on July 31, 2001 at 3:29 PM

> Trying to understand the package statement...

> I'm new to Java and I'm studing the book, Thinking in Java.
> I've read Chapter 05 and working on exercise 12.
> ******************************************************************
> 12. Create the following file in the c05/local directory
> (presumably in your CLASSPATH):
> ///:
> package c05.local;
> class PackagedClass {
> public PackagedClass() {
> System.out.println(
> "Creating a packaged class");
> }
> } ///:~
> Then create the following file in a directory other than c05:
> ///:
> package c05.foreign;
> import c05.local.*;
> public class Foreign {
> public static void main (String[] args) {
> PackagedClass pc = new PackagedClass();
> }
> } ///:~
> Explain why the compiler generates an error. Would making the Foreign
> class part of the c05.local package change anything?
> ******************************************************************

> I know that the compile error is because the PackagedClass is not
> public. The Foreign class is in another package.

> Now I want to go off on a "different angle" from all of that. I want to make it
> work be making the PackagedClass public. From my reading that makes the class available to
> other classes even if they're in another package. Am I correct in thinking that?
> If not, that would explain everything. If I am correct please read on...

> I made it public and could then it would compile. I put
> in c05\foreign. The c05 is in my classpath.

> Let me make this part clear:

> CLASSPATH=.;C:\SQLLIB\java\;C:\SQLLIB\java\;d:\JavaStuff

> The d:\JavaStuff contains a c05 sub. That sub contains foreign and local.

> What I'm finding now is that I can compile the file but it will not run.

> I finally commented out the package statement in and it ran as expected.

> If I don't comment out the package statement in I see:

> D:\JavaStuff\c05\foreign>java Foreign
> 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(Unknown Source)
> at Source)
> at Source)
> at$100(Unknown Source)
> at$ Source)
> at Method)
> at Source)
> at java.lang.ClassLoader.loadClass(Unknown Source)
> at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
> at java.lang.ClassLoader.loadClass(Unknown Source)
> at java.lang.ClassLoader.loadClassInternal(Unknown Source)


I would suggest you run the java program from the d:\javaStuff directory in this way
java c05.foreign.Foreign
that should work probably


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us