Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Arghhh! I give up, why doesn't this easy thing work?

1 view
Skip to first unread message

Jason

unread,
Jan 8, 2001, 9:11:13 PM1/8/01
to
Hello. I'm having trouble with a very simple program. I know there is
some simple mistake but I can't figure it out. All I'm trying to do is
define an interface in one package and implement it in a class in
another package. I have the following two files:

C:\LearnJava\TIJ\Chapter8\Ex2\Package1\Ex2Interface.java the contents of
which follows

package Ex2.Package1;

public interface Ex2Interface
{
public void Method1( );
public void Method2( );
public void Method3( );
}


and C:\LearnJava\TIJ\Chapter8\Ex2\Package2\Ex2Imp.java the contents of
which follows

package Ex2.Package2;
import Ex2.Package1.*;

public class Ex2Imp implements Ex2Interface
{
public void Method1( ) { System.out.println("Method1
Implementation"); }
public void Method2( ) { System.out.println("Method2
Implementation"); }
public void Method3( ) { System.out.println("Method3
Implementation"); }

public static void main(String[] args)
{
Ex2Imp E = new Ex2Imp( );

E.Method1( );
E.Method2( );
E.Method3( );
}
}

I compile the interface successfully by cd'ing to the Package1 directory
and issueing: javac Ex2Interface.java

I then compile the implementation successfully by cd'ing to the Package2
directory and issueing: javac -classpath C:\LearnJava\TIJ\Chapter8
Ex2Imp.java

The problem arises when I try to run Ex2Imp.java by issueing: java
-classpath C:\LearnJava\TIJ\Chapter8 Ex2Imp

I get the following exception:

Exception in thread "main" java.lang.NoClassDefFoundError: Ex2Imp

If I issue the java command withouth the classpath option I get
something a little different:

Exception in thread "main" java.lang.NoClassDefFoundError: Ex2Imp (wrong
nam

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 apologize for being so long winded here, but I wanted to be sure to
include all of the details. I'm really pulling my hair out over what I
am sure is a small mistake. Thanks in advance for any help anyone can
offer.

-Jason

Madhusudhanan Challur

unread,
Jan 8, 2001, 9:40:20 PM1/8/01
to
While executing the class you have to use:
java -classpath C:\LearnJava\TIJ\Chapter8 Ex2.Package2.Ex2Imp

Jason <jas...@optonline.net> wrote in message
news:3A5A2D4B...@optonline.net...

Jason

unread,
Jan 8, 2001, 10:09:47 PM1/8/01
to
Thanks! It worked like a charm (though it's not quite clear to me why the
qualification is necasarry if you're already in the directory).

-Jason

Jon Skeet

unread,
Jan 9, 2001, 3:40:37 AM1/9/01
to
jas...@optonline.net wrote:
> Thanks! It worked like a charm (though it's not quite clear to me why the
> qualification is necasarry if you're already in the directory).

Because the class you're trying to run *isn't* called Ex2Imp. It's
called Ex2.Package2.Ex2Imp.

Rather than compiling things from the directory they're in, do
*everything* from a "base" directory:

C:\LearnJava\TIJ\Chapter8> javac Ex2\Package2\*.java

C:\LearnJava\TIJ\Chapter8> java Ex2.Package2.Ex2Imp

--
Jon Skeet - sk...@pobox.com
http://www.pobox.com/~skeet

Amal Banerjee

unread,
Jan 9, 2001, 3:34:22 PM1/9/01
to Jason

In the concrete class which implements the interface, one must have:
public class Ex2Imp implements <Appropriate_package_name>.Ex2Interface

On Tue, 9 Jan 2001, Jason wrote:

0 new messages