Exception in thread "main" java.lang.NoClassDefFoundError - Java2sdk1.4.2_06

I've obviously a newbie to java, *ATTEMPTING* to teach myself, but I've run into a problem that's really held me up.

As you can see below, I'm attempting to run a java application called "helloworldapp".  helloworldapp.class (and .java) is located in c:\j2sdk1.4.2_06\bin\helloworldapp.
It compiles just fine but will not run.  I think there may be a classpath problem, but I've changed the path a ton of times and can't get it right.


C:\j2sdk1.4.2_06\bin\HelloWorldApp>javac helloworldapp.java

C:\j2sdk1.4.2_06\bin\HelloWorldApp>java helloworldapp
Exception in thread "main" java.lang.NoClassDefFoundError: helloworldapp (wrong
name: HelloWorldApp)
        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)

 Volume in drive C has no label.
 Volume Serial Number is 549D-DF6C

 Directory of C:\j2sdk1.4.2_06\bin\HelloWorldApp

10/22/2004  03:46 PM    <DIR>          .
10/22/2004  03:46 PM    <DIR>          ..
10/22/2004  04:17 PM               432 HelloWorldApp.class
10/22/2004  03:59 PM               157 HelloWorldApp.java
               2 File(s)            589 bytes
               2 Dir(s)   3,418,599,424 bytes free

Running WinXP SP2, and here's what I have for my environmental variables:

User variables:
CLASSPATH - .;C:\j2sdk1.4.2_06\bin
PATH - .;C:\j2sdk1.4.2_06\bin

System variables:
CLASSPATH - .;C:\j2sdk1.4.2_06\lib\tools.jar;C:\j2sdk1.4.2_06\jre\lib\rt.jar;C:\j2sdk1.4.2_06\jre\classes
JAVA_HOME - c:\j2sdk1.4.2_06
Path - %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\NMapWin\\bin;"C:\Program Files\Symantec\Norton Ghost 2003\";C:\j2sdk1.4.2_06\bin;JAVA_HOME;

I found many instances of this issue here and elsewhere and have tried most of their suggestions, and now I'm afraid I may have just completely messed it all up!

Please help!
Who is Participating?
CEHJConnect With a Mentor Commented:
Java of course is case-sensitive, so the class name must be exactly as declared in the class file, and the same as the file name

java -classpath . helloworldapp
As the exception suggests - you have a problem with names.
Try renaming the helloworldapp.java file to HelloWorldApp.java and running "java HelloWorldApp" .
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

are you running your command from this path
C:\j2sdk1.4.2_06\bin\HelloWorldApp>java helloworldapp

check if your helloworldapp.java has got any package?? if it has any package you need to create the folders as in the package names in your java files and then place the class file in the last folder of your package. Then you can run ur app like

C:\j2sdk1.4.2_06\bin\HelloWorldApp>java com.helloworldapp.helloworldapp

the folder from where u run the command should also have the package starting  for your java class.
 so like in the above command if your package is com.helloworldapp then u should be having folders

pl check out all these and test again.
hope this helps

See CEHJ's comment, if still no luck, try this:
just add this line, and press enter, before running (and preferably prior to compiling) your file:

set classpath=%classpath%;%cd%

Best of luck,
Heres what your problem is...
 as suggested by CEHJ you are using wrong class name

You are using java helloworldapp

while the contents of the directory say its HelloWorldApp.class

so You should be using java HelloWorldApp

Here is a simple scenario that I tried without looking much into the problem and it even sounds the same....

Your file name is helloworldapp.java

class HelloWorldApp
public static void main(String[] args)

It compiles and when U look at the contents of the directory U will get
Now try this in same file removing the above contents

public class helloworldapp
public static void main(String[] args)

It compiles as the public class name and file name are same but If You look at contents of directory You should get

but surprisigly I found HelloWorldApp.class instead of helloworldapp.class( I donno why this is happening)

but to execute it I need to give java helloworldapp

If I give java HelloWorldApp I get the following error

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp (wrong
name: helloworldapp)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:605)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:290)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:279)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:236)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:303)

Which is exactly the error shown above......

So open HelloWorldApp.java

check the class name that contains main...

and then use that classname after java while trying to run.
run it using the following:

java HelloWorldApp

(your classpath is fine, and everything else appears fine)
That's already been said (several times)
Not directly (in fact only Ori did actually), and the above comments primarily talk about changing the classpath and filename which are all unnecessary.
However reliable the evidence of the classpath that's *actually being used is* (and that shouldn't be assumed to be 100% correct) all the information necessary to get this working has already been given
Not to mention that it is not necessary to have the environment variable classpath set. I had a similar problem when I started coding in java, and fixed it by deleting the classpath env variable

So what exactly is your problem, with my clarifying what I believe to be the correct approach to run the command? As well as useful information, there is also a lot of erroneous information posted which could well confuse the asker. For eaxample, the suggestion that you posted will certainly *not* work.

>>So what exactly is your problem, with my clarifying what I believe to be the correct approach

Simply that it's unnecessary. Your comments simply repeat what Ori Kremer said, which was the specific form of my more generic second comment
CEHJ & objects, don't fight! It's becoming a habit!

lol - last time I had a disagreement with someone, I was threatened that I will loose my account for it! :o\

By whom? Only moderators may tell you so.
tebacher, next time you have such a problem, post your source;
it would have made this all easier and shorter.
Yup -- it was moderators!
By the way, putting your java app in "C:\j2sdk1.4.2_06\bin" is not a good idea.
Using a directory of your own is much better (something like "C:\MyJavaProjects\HelloWorld").
tebacherAuthor Commented:
Wow, lots of help in this TA!  Thanks to everyone who has helped so far.

I *swear* I tried 'java HelloWorldApp' (with Caps as they should be) before I posted here!  But, after the weekend I booted up my PC and tried it with the caps, and it worked.   Just a case problem  :-)
Maybe I tried the Caps BEFORE I got the correct class path in and then just disregarded it from then on.

Anyway, thanks for the help!
Aside from all the others' comments,

I suggest you to use an IDE for java..

some popular ones includes the paid ones such as borland, or JCreator Pro.

while the free ones such as Gel.

the IDE helps you a lot when it comes to memorization of different java packages and therefore allows you to easily learn Java.
> Aside from all the others' comments,
> I suggest you to use an IDE for java..
> some popular ones includes the paid ones such as borland, or JCreator Pro.
> while the free ones such as Gel.

You must be kidding!?! Don't even think of looking further than the Eclipse IDE!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.