what is the "java.lang.NoClassDefFoundError" ?

X

xian_hong2046

Hello,

After I successfully compiled my program and ran it, I received the
message:

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

I double-checked my program and there's no mistake at all. This
problem happened after I set the CLASSPATH variable, which had no
setting before. I also noticed that my other programs that worked
before failed to work any more, with the same exception as shown above.
Could someone please tell me what's the problem and how to solve it?

many thanks,
xian
 
M

MysteryMan

Try the following from the command line to run your program:

java -classpath . YourClassName

Don't forget that "." in there.
 
X

xian_hong2046

Many thanks for your reply and with the -classpath option my previous
programs work. However, my new program,which requires a package, still
fails to work.

My problem is that I have a package called mypackage.mytest, and inside
this package I have MyFirstTest.java that has MyFirstTest class. Then
in the second file TestTester.java, I import the mypackage.mytest and
use a static method in the MyFirstTest class. Then I compiled
MyFirstTest and TestTester files separately. Everything went on
properly until I used java:

java -classpath . TestTester

when I got the previous error message.

Moreover, I set my CLASSPATH to a subdirectory in my home directory. I
expect that Java interpreter should be able to find it. Why do I still
need to use "-classpath" option?

In addition, before I ran TestTester I had moved TestTester.class and
MyFirstTest.class into the correct directory (corresponding to the
package name), so I think this should eliminate the possibility that
Java interpreter can't find the .class in the package directory.

Many thanks,
xian
 
J

Jon Martin Solaas

Many thanks for your reply and with the -classpath option my previous
programs work. However, my new program,which requires a package, still
fails to work.

My problem is that I have a package called mypackage.mytest, and inside
this package I have MyFirstTest.java that has MyFirstTest class. Then
in the second file TestTester.java, I import the mypackage.mytest and
use a static method in the MyFirstTest class. Then I compiled
MyFirstTest and TestTester files separately. Everything went on
properly until I used java:

java -classpath . TestTester

when I got the previous error message.

Moreover, I set my CLASSPATH to a subdirectory in my home directory. I
expect that Java interpreter should be able to find it. Why do I still
need to use "-classpath" option?

In addition, before I ran TestTester I had moved TestTester.class and
MyFirstTest.class into the correct directory (corresponding to the
package name), so I think this should eliminate the possibility that
Java interpreter can't find the .class in the package directory.

Many thanks,
xian

You shouldn't need the -classpath if your $CLASSPATH is correct. But it
seems it is not. And adding the . doesn't help all that much, because
still you haven't gotten the classpath pointing to the package right.

Suppose you have someting like

/home/me/classes/mypackage/mytest/MyFirstTest.class

Then you should have /home/me/classes in classpath if MyFirstTest is
declared to reside in the mypackage.mytest package. You can achieve that
either via -classpath or by setting the CLASSPATH environment variable.
The latter is somewhat more cumbersome in my opinion. You'd still have
to keep the . so eventually it should look something like

java -classpath .:/home/me/classes TestTester
 
X

xian_hong2046

Thanks to all of you, I've solved the problem. There's just one more
problem that I can't figure out why. I tried to import an entire
package, using the "*" syntax. However, the compiler complained it
couldn't find the class. The problem disappeared after I explicitly
imported the classes stored in that package. Could someone tell me why
this happened please?

Thanks,
xian
 
B

Bjorn Abelli

Thanks to all of you, I've solved the problem. There's just one more
problem that I can't figure out why. I tried to import an entire
package, using the "*" syntax. However, the compiler complained it
couldn't find the class. The problem disappeared after I explicitly
imported the classes stored in that package. Could someone tell me why
this happened please?


My guess is that you thought that "subpackages" automatically will be
included when you use the *.

If you want to get a better answer, please provide more information of the
problem at hand, in this case:

- The "import part" of your source code.

- The *exact* error message (including what class
it complains about not to find).

We're good at guessing, but not *that* good...

// Bjorn A
 
X

xian_hong2046

Hello,

Indeed, I should have provided you with more information. :)

I have two simple classes, the first one is:

package mytest;

public class PackageTest1{
public static void print(){
System.out.println("hello, in PackageTest1.");
}
}

Then in the second one I have:

import mytest.*;

public class PackageTest2{
public static void main(String[] args){
PackageTest1.print();
}
}

After I compiled PackageTest1.java and moved its .class into the proper
directory, I tried to compile PackageTest2.java. The compiler's error
message was:

PackageTest2.java:5: cannot access PackageTest1
bad class file: ./PackageTest1.java
file does not contain class PackageTest1
Please remove or make sure it appears in the correct subdirectory of
the classpath.
PackageTest1.print();
^
1 error

Finally, I replaced the "import mytest.*;" by "import
mytest.PackageTest1;" and everything worked out.

If the compiler could find mytest.PackageTest1, then surely it should
find mytest.*?

Thanks!
xian
 
B

Bjorn Abelli

After I compiled PackageTest1.java and moved its .class
into the proper directory, I tried to compile PackageTest2.java.
The compiler's error message was:

PackageTest2.java:5: cannot access PackageTest1
bad class file: ./PackageTest1.java

There you got it!

It doesn't complain about PackageTest1.class, but on PackageTest1.java.

Move PackageTest1.java out of the classpath...
If the compiler could find mytest.PackageTest1, then
surely it should find mytest.*?

AFAIK, it works approximately this way:

When you explicitly declare PackageTest1 to belong to the package, it looks
there first, otherwise it looks through the whole classpath for
PackageTest1.class *AND* PackageTest1.java.

In that case (mytest.*) it isn't sure what package PackageTest1 belongs to,
and searches the whole classpath. Then it found a .java-file in the
*default* package and tries to work with that, but discovers that it defines
a class *not* belonging to the *default* package, hence a "bad class
file"...

// Bjorn A



Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top