The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
July 2001

Advertisement

Advertisement

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.

Message:

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):
> ///: c05:local:PackagedClass.java
> 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:
> ///: c05:foreign:Foreign.java
> 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
> Foreign.java in c05\foreign. The c05 is in my classpath.

> Let me make this part clear:

> CLASSPATH=.;C:\SQLLIB\java\db2java.zip;C:\SQLLIB\java\runtime.zip;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 foreign.java file but it will not run.

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

> If I don't comment out the package statement in foreign.java 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 java.security.SecureClassLoader.defineClass(Unknown Source)
> at java.net.URLClassLoader.defineClass(Unknown Source)
> at java.net.URLClassLoader.access$100(Unknown Source)
> at java.net.URLClassLoader$1.run(Unknown Source)
> at java.security.AccessController.doPrivileged(Native Method)
> at java.net.URLClassLoader.findClass(Unknown 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



Replies:

Sponsored Links



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