classpath idiot

J

justdoit693

I've been programming java for a while now so i'm not a novice (although
this problem is making me feel like one)...heres the scenario. In fact,
i've done all this before so i'm not sure what i'm missing here.

I've got a JAR file that acts as a library that contains an interface called
com.app.CallbackInterface

I have another class that takes an implementation of a CallbackInterface in
the same JAR file and the same path.

Now i've created a test application with this basic code.

import com.app.* ;

public class MyTestApp implements CallbackInterface {
public static void main(String args[]) {
AppMonitor app = new AppMonitor(new MyTestApp()) ;

}

// callback interface methods go here...

}

The code compiles just fine but I get a NoClassDefFoundError when I try to
run the app.

Exception in thread "main" java.lang.NoClassDefFoundError:
com/app/CallbackInterface
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)

Of course, i've got my class path pointing to the JAR file mentioned above
(the code wouldn't compile if I didn't). FYI, i'm using ANT with a single
classpath configured and my <javac> task refers to the path by reference.
Also, just for convenience I have a run target that uses the same
classpathref for the <java> task as the <javac> task so I know they are both
using the correct classpath. I've also setup a batch file that creates the
correct classpath in case i'm messing up with ANT somehow...

I'm completely stumped at this point. What am I missing?

thanks in advance for any help.
 
M

Moshe Sayag

You have to add the newly generated classes to the classpath of the
<java> target?

for example:
javac -classpath mylib.jar -d out CallbackInterface
java -classpath mylib.jar;out CallbackInterface

Notice the the different classpath arg.

Moshe
 
J

Joseph Millar

Of course, i've got my class path pointing to the JAR file mentioned above
(the code wouldn't compile if I didn't). FYI, i'm using ANT with a single
classpath configured and my <javac> task refers to the path by reference.
Also, just for convenience I have a run target that uses the same
classpathref for the <java> task as the <javac> task so I know they are both
using the correct classpath.

java and javac can use CLASSPATH and its variants in slightly
different ways. And ant may well have some setting you don't
expect, either on the configured command line or in the
environment.
I've also setup a batch file that creates the
correct classpath in case i'm messing up with ANT somehow...

That's good, eliminate ANT so you can precisely set and see what
the CLASSPATH is set to. I would use a bare command line with
-cp to run your app and see if it succeeds. Here are the steps
I would take:

1. Go to a command line.
2. Unset CLASSPATH environment variable (so as not to find
anything unexpected).
3. Locate all the pieces that the app will need in order to run.
3. Run your app using "java -cp myjar.jar;<other stuff> MyTestApp".
Replace "<other stuff>" with whatever else your app needs in
order to run. Does it run? If not, what is it missing?
4. Then start adding CLASSPATH items back in until it starts
working.
5. Figure out exactly why a particular item causes things to work
or not work.

I suspect that you will find one of two things.

a) You have a bad copy of CallbackInterface lying around some-
where that is either not in a package, or in a different
package,

or

b) Your CLASSPATH setting in ANT is not what you think it is.
I'm completely stumped at this point. What am I missing?

You don't provide any concrete details on what your classpath
settings are for us to be specific, so you will have to trace
it down, or give us more info, like exactly what the commands
are, and what the env settings are.

--Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top