• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

NoClassDefFoundError

 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do i get this error when i try to invoke the main method using the following command (at Dos prompt):

>java MyClass

My CLASSPATH is set and so is my PATH (to point to the \bin directory of my java version.

??
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check these:

a) MyClass.java has no package statement
b) MyClass.java has a public static void main(String[] args) method.
c) MyClass.java compiled with no errors
d) MyClass.class is in the current directory

If those are all true, it oughtta work.

If you have a package statement the package name should match a directory tree. If your package is com.myname.myproject then MyClass.class should be in com/myname/myproject.

Any of those help?
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The CLASSPATH should not point to the bin directory of your JVM. It will work if you point to the folder containing MyClass.class, however use of CLASSPATH is considered poor practice.

Try this: java -classpath . MyClass

It's always, always, always better to explicitly set your classpath on the java command line. Now, in case I negleted to qualify that, let me mention that it's always better to specify it directly on the command line. Oh, I see I repeated myself. No worries, it's worth emphasizing
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to be nitpicky...

Originally posted by Stan James:
Check these:
...
d) MyClass.class is in the current directory

If those are all true, it oughtta work.



But Marzo said that CLASSPATH is set. So either . (the current directory) has be in CLASSPATH, or MyClass.class has to be in one of the directories in CLASSPATH.

for Windows:
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It's always, always, always better


While I agree entirely, I can hear the hippies in the background mumbling the typical "you can make absolutes like that" whilst toking on a joint. Unfortunately, this remark contradicts a great many scientists and philosophers in history who have dedicated their lives to making hypotheses on some basis, which results in "absolutes" (just what is one of these anyway? This is a rhetorical question). Now technically, these hippies are right; all absolutes require context, and that has not been provided, but ironically, it is assumed by all parties involved. Given that quite often contexts cannot be provided in English since it is too inaccurate (this can be proven as an absolute in a given context with requirement analysis techniques), the best case is a paraphrase. I'll leave that up to you (whoever) to decide on, my suggested axiom is something along the lines of "you want to have a robust build environment and ensure that nothing breaks or is inadvertantly corrupted... yaddy ya (to be continued)". On the contrary, a context where the statement may be contradicted is perhaps "you wish to have an untrustworthy build environment whose results are apparantly random or left at the discretion of any ol' goose who accesses the build environment". You cannot rule out the latter case as a possibility; just that in practice, it seldom occurs.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say to said hippies (hippae?) that never-say-never is always implied whether explicitly stated or not � [audible huh?]. BTW, the reason I say always, always, always is my limited vocabulary. Repeating the same adjective is an inefficient but effective means of adding emphasis when the superlative is unknown. A mixed strategy can also be employed: super, super, super duper!
[ August 21, 2005: Message edited by: Rick O'Shay ]
 
Master Rancher
Posts: 5112
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Back to the OPs problem.
Could you post the full contents of the screen when you get the error?
There is a bit more info available there:
Here's an example:


D:\JavaDevelopment>java asdf
Exception in thread "main" java.lang.NoClassDefFoundError: asdf


Also if it's Windows, enter the dir command to show the contents of the current directory.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW: Does anybody know the reason, why you have to set '.' in the CLASSPATH on Windows, but not on linux?

I'm working fine since years with no classpath at all, and now I got in touch with Windows again, and 'hooo!' CLASSPATH problems!
Couldn't believe it first.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> why you have to set '.' in the CLASSPATH on Windows, but not on linux?

You're demonstrating one of the problems: the default classpath is the current directory, by definition. Hoever, if you set it, there it goes the default. So, you probably had it set on Windows without "." in the CLASSPATH while it was not set at all on Linux.

Older Java programs will change CLASSPATH. Sometimes putting their stuff first; sometimes adding things on the end. It doesn't exist at all on some platforms. It's just a bad idea all around.
[ August 21, 2005: Message edited by: Rick O'Shay ]
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This should probably be moved to JIG Beginner
[ August 22, 2005: Message edited by: Rick Beaver ]
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

classdefnotfound error appears because ur classpath is not set properly.

suppose assume your .java file is present in c:\test\oo.java.

u should set your classpath to c:\test.

set CLASSPATH=c:\test.

THis will solve ur problem

Thanks,
Chandra
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.lang.NoClassDefFoundError occurs because a class loader cannot find the class that has been requested to load. It has nothing at all to do with source files, and only indirectly and within some context, does it have anything to do with the CLASSPATH environment variable, though it is a reasonable assumptions that this context applies here.

As has already been pointed out, setting the CLASSPATH environment variable should never (within quite a broad context) be done. Unset it, then try again; if that fails, post the exact issue that you are experiencing.
 
Marcelo Ortega
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone, you are all very kind and helpful.

Regards.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by shekar march chandra:
Hi,

set CLASSPATH=c:\test.

THis will solve ur problem

Thanks,
Chandra



Actually, that will cause more problems if there are already programs relying on CLASSPATH. So your suggestion, although it agrees with all the other correct solutions that preceded it, is sub-optimal.

I know this is a hard one to shake loose because most books and examples show setting the CLASSPATH, but don't set it if possible. Use -classpath. I can't say because my lips are sealed, but when it comes time for the SCJP test, you might very will pass or fail based on your knowledge of the javac and java command line switches, particularly -classpath.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rick O'Shay:
>> why you have to set '.' in the CLASSPATH on Windows, but not on linux?

You're demonstrating one of the problems: the default classpath is the current directory, by definition. Hoever, if you set it, there it goes the default. So, you probably had it set on Windows without "." in the CLASSPATH while it was not set at all on Linux.
[ August 21, 2005: Message edited by: Rick O'Shay ]



No, there was no CLASSPATH set on Win (Xp), nor on linux.
echo %CLASSPATH%
(nothing - de rien - nix)
echo $CLASSPATH
(nothing - de rien - nix)

Another difference: On both systems, I have set $JAVA_HOME (%JAVA_HOME%) to the java-home.
On linux, I can put often used jars into $JAVA_HOME/jre/lib/ext and don't need to specify them in the classpath.
On windows that is of no help.

Hm.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If CLASSPATH is not set then, by definition, the default classpath is the current directory. Something else is wrong. I tried it on both systems and classes in my current directory were picked up. No CLASSPATH (never set it anyway) and no -classpath.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stefan Wagner:
Another difference: On both systems, I have set $JAVA_HOME (%JAVA_HOME%) to the java-home.
On linux, I can put often used jars into $JAVA_HOME/jre/lib/ext and don't need to specify them in the classpath.
On windows that is of no help.


It can be done on Windows. You need to specify the path (without using %JAVA_HOME% because that's not where the jre is). It's usually something more like C:\Program Files\Java\jre1.4.2_06\lib\ext
 
Stefan Wagner
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@marilyn:

You mean I need to specify the ext-directory in the PATH?

I've never tried before, and don't do on linux, but I will test it, as soon as I boot windows.
 
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic