Two Laptop Bag*
The moose likes Java in General and the fly likes NoClassDefFoundError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "NoClassDefFoundError" Watch "NoClassDefFoundError" New topic
Author

NoClassDefFoundError

Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

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.

??


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
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
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1007
    
    3
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:
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

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.


Tony Morris
Java Q&A (FAQ, Trivia)
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
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 ]
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
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.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

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.


http://home.arcor.de/hirnstrom/bewerbung
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
>> 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 ]
Rick Beaver
Ranch Hand

Joined: Dec 14, 2004
Posts: 464
This should probably be moved to JIG Beginner
[ August 22, 2005: Message edited by: Rick Beaver ]

ph34r my 133t j4v4 h4><0r1ng sk177z
shekar march chandra
Greenhorn

Joined: Jul 07, 2005
Posts: 25
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

Joined: Sep 24, 2003
Posts: 1608
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

Joined: May 31, 2005
Posts: 519

Thanks everyone, you are all very kind and helpful.

Regards.
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
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

Joined: Jun 02, 2003
Posts: 1923

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

Joined: Sep 19, 2004
Posts: 531
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.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
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


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

@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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NoClassDefFoundError