Solved

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

Posted on 2004-10-22
87,586 Views
Last Modified: 2012-06-21
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)

C:\j2sdk1.4.2_06\bin\HelloWorldApp>dir
 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!
0
Question by:tebacher
    23 Comments
     
    LVL 86

    Expert Comment

    by:CEHJ
    Try

    java -classpath . helloworldapp
    0
     
    LVL 86

    Accepted Solution

    by:
    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
    0
     

    Expert Comment

    by:Ori_Kremer
    As the exception suggests - you have a problem with names.
    Try renaming the helloworldapp.java file to HelloWorldApp.java and running "java HelloWorldApp" .
    0
     
    LVL 8

    Expert Comment

    by:kiranhk
    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
    C:\j2sdk1.4.2_06\bin\HelloWorldApp>
    ---------------->com------->helloworldapp

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

    0
     
    LVL 9

    Expert Comment

    by:DrWarezz
    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,
    [r.D]
    0
     
    LVL 2

    Expert Comment

    by:ThummalaRaghuveer
    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)
    {
    System.out.println("Hello");
    }
    }

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

    public class helloworldapp
    {
    public static void main(String[] args)
    {
    System.out.println("Hello");
    }
    }

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

    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
    4)
            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.
    0
     
    LVL 92

    Expert Comment

    by:objects
    run it using the following:

    java HelloWorldApp

    (your classpath is fine, and everything else appears fine)
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    That's already been said (several times)
    0
     
    LVL 92

    Expert Comment

    by:objects
    Not directly (in fact only Ori did actually), and the above comments primarily talk about changing the classpath and filename which are all unnecessary.
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    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
    0
     
    LVL 3

    Expert Comment

    by:oratim
    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

    0
     
    LVL 92

    Expert Comment

    by:objects
    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.

    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    >>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
    0
     
    LVL 24

    Expert Comment

    by:sciuriware
    CEHJ & objects, don't fight! It's becoming a habit!

    ;JOOP!
    0
     
    LVL 9

    Expert Comment

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

    0
     
    LVL 24

    Expert Comment

    by:sciuriware
    By whom? Only moderators may tell you so.
    ;JOOP!
    0
     
    LVL 24

    Expert Comment

    by:sciuriware
    tebacher, next time you have such a problem, post your source;
    it would have made this all easier and shorter.
    ;JOOP!
    0
     
    LVL 9

    Expert Comment

    by:DrWarezz
    Yup -- it was moderators!
    0
     
    LVL 30

    Expert Comment

    by:GrandSchtroumpf
    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").
    0
     
    LVL 6

    Author Comment

    by:tebacher
    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!
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    8-)
    0
     
    LVL 6

    Expert Comment

    by:SamsonChung
    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.
    0
     
    LVL 1

    Expert Comment

    by:SchmidlinF
    > 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!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Product Review - Android Remix

    Come along for the ride with our Senior Product Manager, Brian Matis, as he reviews the Android Remix.

    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    933 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now