Java: Exception in thread main java.lang.NoClassDefFoundError

Contributor Icon Contributed by davak Date Icon February 12, 2005  
Tag Icon Tagged: Java programming

Common error that beginning java authors may experience.

    Exception in thread “main” java.lang.NoClassDefFoundError:

    You try to run a little java program and you get this blasted error. I first got this error when running our little hello world program.

    This occurs because the classpath is not setup or referenced correctly.

    Executing your program using this command should correct the problem:
    java -classpath . helloworld

    where helloworld is the name of your compiled class.

    This tells java that your classpath is your local directory.

Previous recipe | Next recipe |
 
  • LP
    Very helpful! I was stuck on this.. Thanks
  • Mimi
    My Classpath looks right, but I still get the error: Exception in thread "main"

    .;C:\Program Files\Java\jdk1.6.0_01\lib
  • Rajesh
    the classpath should include the directory in which u r keeping ur own class files.
  • Andy
    I'm sorry it did not work.
  • chandu
    if u have stored ur classes in the directory other than JDK installed...we need to set path to the bin dir by using
    set path=.....
    and for classes
    set classpath=....
  • Thank you!! It really helped me.
    i just copied my files in bin dir. try doing this.
  • harpreet
    thank you it really helped me
  • CP
    I have the same issue Exception in thread "main" java.lang.NoClassDefFoundError: any luck
  • v
    Just call this at the directory where you classfile XYZ.class is located :

    java -classpath . XYZ
  • karep
    salam

    didn't work for me ...
    any idea ?? :'(

    wassalam
  • Nick
    I had a problem with that, and then I noticed, that I had written"java -classpath . XYZ.class" instead of "java -classpath . XYZ" Don't know, if this helps you, but worked for me :)
  • dhaulakhandi
    hey bro's thnx it solved my issue
  • springrider
    Thanks a lot! It help me!
  • abhishek
    Microsoft Windows [Version 6.0.6001]
    Copyright (c) 2006 Microsoft Corporation. All rights reserved.

    C:\Users\mdube>cd\

    C:\>javac C:\abhishek\Mohit.java

    C:\>java C:\abhishek\mohit
    Exception in thread "main" java.lang.NoClassDefFoundError: C:\abhishek\mohit

    C:\>java C:\abhishek/mohit
    Exception in thread "main" java.lang.NoClassDefFoundError: C:\abhishek/mohit

    C:\>javac C:\abhishek/Mohit.java

    C:\>$Recycle.Bin
    '$Recycle.Bin' is not recognized as an internal or external command,
    operable program or batch file.

    C:\>JAVAC
    Usage: javac <options> <source files>
    where possible options include:
    -g Generate all debugging info
    -g:none Generate no debugging info
    -g:{lines,vars,source} Generate only some debugging info
    -nowarn Generate no warnings
    -verbose Output messages about what the compiler is doing
    -deprecation Output source locations where deprecated APIs are us
    ed
    -classpath <path> Specify where to find user class files
    -sourcepath <path> Specify where to find input source files
    -bootclasspath <path> Override location of bootstrap class files
    -extdirs <dirs> Override location of installed extensions
    -d <directory> Specify where to place generated class files
    -encoding <encoding> Specify character encoding used by source files
    -source <release> Provide source compatibility with specified release
    -target <release> Generate class files for specific VM version
    -help Print a synopsis of standard options


    C:\>JAAVA
  • Tzell
    I am using JGrasp as a new Java learner and I hate it. The errors given when compiled to vague. Can anyone suggest a better application to begin learning and developing in?
  • FrederikNS
    the errors are thrown by the Sun Java Compiler, and that doesn't differ regardless of which developer environment you try to program in, as long as it's java, you'll have to stick with it.
    But some of the best environment to program java in would have to be Eclipse or NetBeans.
    They have an awful lot of features, and is very easy to use when you get over the initial general troubles, such as which features to use, and getting an overview of all the stuff they can do.
  • asif
    Yes it worked in eclipse!! thanks.
  • stacy
    try netbeans
  • ahmad aghajani
    My Classpath looks right, but I still get the error: Exception in thread "main"

    .;C:\Program Files\Java\jdk1.5.0_01\lib
  • Jim Thomas
    The above comment did not work. For newbies it may be easier to save the source files in the bin directory eliminating any path issues.
  • chandu
    we need to set two paths
    1. for JDK use following command
    set path=C:\......
    2. for classes where u compiled and kept
    set classpath=D:\prgrams......
  • grace
    you say executing your program using ... pls how exactly do i execute it. i need the basic steps
  • Sushil Karane
    java page we get the error as follows :

    Exception in thread "main" java.lang.UnsupportOperationException at sun.jdbc.odbc.jdbcOdbcConnection.createStatement(Unknown Source)
    at DBConn.<init><DBConn.java,Compiled Code>
    at ReceiveMail.<init><Receivemail.java,Compiled Code>
    at insertion.main(insertion.java:10)
  • For my CLASSPATH I have :
    C:\Program Files\Java\jdk1.6.0_10\jre\lib;C:\Program Files\Java\jdk1.6.0_10\jre\bin;C:\novell\ndk\ndssdk\java\lib\ldap.jar;.;C:\JavaProjects\SearchLDAPTest
    Where C:\JavaProjects\SearchLDAPTest is where my List.class compiled class resides

    I also created JAVA_HOME Variable for my user with a value of:
    C:\Program Files\Java\jdk1.6.0_10\

    and finally I have for my Path:
    C:\Program Files\Java\jdk1.6.0_10\jre\bin;D:\Program Files\SSH Communications Security\SSH Secure Shell;C:\Program Files\Java\jdk1.6.0_10\bin\javac.exe;C:\novell\ndk\njclv2r\src\njcl

    Which C:\Program Files\Java\jdk1.6.0_10\jre\bin is the bin of the RUNTIME or should I also list C:\Program Files\Java\jdk1.6.0_10\bin :: the JDK bin/ instead ?
    anyone with any CLEAR ideas ? thx
  • Abhhilash
    I have created the hello world program and i am using textpad to do that
    My program is compiling but when i run it gives me an error
    java.lang.NoClassDefFoundError:
    Exception in thread "main"
    Tool completed with exit code 1
    I have set my path to C:\jdk1.3\bin
    and the classpath to where the program reside but in vein can anybody help
  • irene
    noooooo.... i have tried it all.... :(
    it keeps saying that java.lang.noclassdeffounderror thing...
  • Nathiya S
    how do create path to j2sdk1.4.2_02
    i run small prgm, my error is
    exception in thread "main" java.lang.noclassdefFoundError

    how do clear my error
  • Martin
    I had the same problem. It turned out the cause was that I tried to compile a java sample file from a read-only folder.
    When I copied the .java file to my hard drive the problem went away
  • Rakesh
    Thanks great info
  • bothanks
    omfg ahmad aghajani ,
    i o u big!!!

    ty for suggesting at putting that in the Class path file, a program of mine has been corrupted for over 2 yrs, and i tried to fix it 1 last time, and ur suggestion worked!!

    ty!!
  • if After setting the path also it doesnt work.. save all files in bin dir and run the prgram using that path.
  • Neicey
    Here is how i fixed mine.
    The .Java and the .Class file is located in C:\Java . i got the above error and changed directory at the dos prompt by typing
    cd Java (Enter)
    Then I compiled by typing the below
    C:\Java>"C:\Program Files\Java\jdk1.5.0_16\bin\javac.exe" C:\java\saluton.java

    Than I run by typing the below
    C:\Java>java Saluton
    Results ...

    Saluton Mundo!

    Hope this Helps . Arichards
  • 007varun
    is it a problem of program?.
  • Sham
    Thanx a lot very usefull!!
  • sad
    After doing the following....where m is my class name

    java -classpath . m
    i get this........

    Unrecognized option: -classpath.m
    Could not create the Java virtual machine.
    help is appreciated!!!1
  • techno
    i think you are not giving space between the classpath n dot n m...
    like java -classpath[space].[space]m

    mine was also having the same error but when i gave spacing between them it worked,,..so u can also try dis once...
  • desperado
    thanks techno, great advice!
  • Su
    Your advice point out what I missed. Thz alot techno.
  • The One
    Ha! I figured it out! I just wasn't compiling every file! "javac *.java" fixed it all! YES!!
  • ut_stud007
    Awesome dude thanks this solved my problem..I used to think that java.exe compiles and executes the application but I gues I was wrong....
  • I had the same problem but your suggestion above did not work. You missed the most obvious problem which is that Java is case sensitive. If you reference the class as Helloworld in the script and then try to run it using java -classpath . helloworld (with the file actually named helloworld.class) then you will get the same error referenced above. The problem is the upper case "H" in the script and then the file starting with a lower case "h".
  • GXOne
    OMG Thankyou After 5 Hours Thankyou.. with that advice it all works exactly the way it should LOL
  • nice
    hi to all

    too , i have error :

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

    help me.
  • Crimson
    hi guys,

    I got this error when i made the mistake of running the class file. Make sure you are running the job correctly (fyi, I'm running vista):

    Correct: C:\Users\Crimson\teaching> java Example

    Incorrect: C:\Users\Crimson\teaching> java Example.class

    This is really easy to forget about, especially if you're used to using an IDE like Eclipse, which was the case for me. I scratched my head for a little while and feel really dumb now.
  • neusbloed
    Crimson you finally made me understand why I was doing it not properly. I use Eclipse too. I'm going to feel really dumb now but I'm really happy I can run my files.
  • desperado
    i used textpad and i still got the same error, can anyone tell me what's wrong?
  • Thomaso
    Thanks sooo much. if the java - classpath . helloworld doesnt work it is probably due to spacing and upper lower case letter problem.

    for me i didnt put a space after java and a space either side of the full stop. and i forgot to write my class with an upper case letter which is how it was saved.

    java(space) -classpath(space) . (space)Welcome1
  • jango19
    Exception in thread "main" java.lang.NoClassDefFoundError: Receiver/class
    Caused by: java.lang.ClassNotFoundException: Receiver.class
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    Could not find the main class: Receiver.class. Program will exit.

    where Receiver is my class..code is getting compiled perfectly but when i run its givin the above error..
  • Ram
    Hi ,

    First set the JAVA_HOME variable with your bin directory.
    then try to run .it will help u.

    Regards
    Ram Yadav
  • priyaj
    hello ,i got a problem while executing a sample java program.
    error is java.lang.NoClassDefFoundError: and im using jdk 1.6 version .
    someone helpout this
  • aravind
    thanq very much for giving the solution.......
  • nabil
    Awesome.
    It worked great
  • hello
    hey im not trieng to program a file or anything like taht. im just trieng to play a private game server and i keep getting the error message Exception in thread "main" java.lang.nodeffound: joader i have no idea how to fix it can anyone help? my CLASSPATH is C:\Program Files\Java\jdk1.5.0_18\bin; and my PATH in C:\Program Files\Java\jdk1.5.0_18\bin


    anyone help?
  • hello
    hey this is me hello again and please dont post anything too confusing i have no knowledge of java except for classpath and path so please keep it simple
  • hello
    and instead of joader its loader sorry for that
  • !!ALERT!! !!ALERT!!

    I just followed every suggestion in this string and no dice...

    FINALLY I GOT IT ----> (on Windows XP) MAKE SURE YOU COMPILE YOUR PROGRAM WITH C:\...javac ClassName.java before you try to execute with C:\...java ClassName

    You will know your program is compiled when you find a ClassName.class file in the same folder as your original ClassName.java program.

    Hope this helps the last desperate few.
    And yes, of course, make sure your path and classpath are correctly configured.
  • Loser
    Worked for me !!!
  • sixlead
    You will also get this error if you append the .class designator when you try to run it. as
    java HelloWorld.class

    java is expecting a class so the .class suffix is not necessary

    of course your class path might be wrong also ;-)
  • Anonymous
    Does not work
  • Ian
    Thank you - this is so helphul for me at the moment

    If you are also dependent on a jar file in the same folder you may need to have something like
    java -classpath .;./dependent.jar helloworld
  • rahul agarwal
    thanks for the replies that have been posted....
    i am so glad that my problem has been solved...
  • Name
    THANK YOU! My God this problem was so annoying
  • daedana
    Gret thank you. I had the problem with the simplest program and the code was published on a book so I thought there can be nothing wrong with it.
    THANK YOU again :)
  • vazeer
    Thank You it really worked for me
  • nahawand
    That worked! Thanks a lot,

    It would probably help if you explicity indicate that the extension ".class" should not be used when using "-classpath" .
  • nahawand
    Ops, should've read the above comments first!

    my bad.. lol
  • JavaNoOb
    Well, I'm currently making my own chatserver in java, and when i compile with cmd, i get this error. Any help? I'm using the guide on Hello World on compiling and making classes.
  • abdul696
    for coming out from this type of error
    we can run the programm by using diffrn style..
    like

    java -cp . className
  • rmitgeek
    It might be due to packages.
    Try removing any "package" from the top of your *.java files.
  • shelli123
    I am getting the class path error.My XYZ.java compiles good..When i run the program i and getting the below error..

    I had set the Classpath for the dependency jar and the Java_HOME = C:\Program Files\Java\jdk1.5.0_20\bin
    C:\Program Files\DDEForJava\examples\com\javaparts\dde\examples>java -cp . Excel
    Async
    Exception in thread "main" java.lang.UnsupportedClassVersionError: ExcelAsync (U
    nsupported major.minor version 49.0)
    at java.lang.ClassLoader.defineClass0(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
    3)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)

    Any help would be appreciated//
    Thanks
    Shelli
  • jatsumo
    C:\javaprog>java HelloWorld
    Exception in thread "main" java.lang.UnsupportedClassVersionError: HelloWorld (U
    nsupported major.minor version 50.0)
    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:\javaprog>
  • arkangel112
    this happening 2 me, now i cant play a runescape private server thats 508, help me please?
  • b00red
    i don't understand this 'Executing your program using this command should correct the problem:
    java -classpath . helloworld' ?
    where / how do i execute with another classpath?
  • Hamhama
    For a beginner you may get this error also if you run:
    java helloworld.class
    Instead try to run:
    java helloworld
  • graysonc
    Okay, here's the mistake I made (which almost drove me crazy):
    set classpath = .;M:\;C:\Program Files\Java\jre1.6.0_14\bin;
    is NOT the same as:
    set classpath=.;M:\;C:\Program Files\Java\jre1.6.0_14\bin;
    If you put spaces around the '=' it won't work. The file would always compile but would only run without the package designation. As soon as I added the 'package' statement at the top I could compile it but it would always give the above error when I tried to run it. Hope this helps someone.
  • dba_dev
    No, java -classpath . helloworld did not work. What next?
  • aatemp
    Thanks, It helped
  • Ankzz
    It works.. One needs to pay heed to what has been said... Follow it step wise and U wont get any error
  • vinayak1
    superb information. excellent !
  • trinitysantosh
    Hi every one.

    I have installed red5 server on my machine and also JDK version of 1_5_0_11

    java -classpath C:\Program Files\Red5\red5.jar F:\Video Chat\red5chatv2.1\JAVA-RED5\src\chat\application.java

    but when I tryed to compile to get my application.class file using above shown command on command promt I got error : Exception in thread "main" java.lang.NoClassDefFoundError: Files\Red5\red5/jar
  • rahullahiri
    thnx...great work buddy.It solved the problem for me.thanks a lot.
  • shobana
    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xml/serializer/TreeWalker
    at org.apache.xalan.processor.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:823)
    at com.pg.EDICustomize.EDISplit204.generateTarget(EDISplit204.java:353)
    at com.pg.EDICustomize.EDISplit204.execute(EDISplit204.java:99)
    at com.pg.EDICustomize.EDISplit204.main(EDISplit204.java:392)
    Caused by: java.lang.ClassNotFoundException: org.apache.xml.serializer.TreeWalker
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    ... 4 more
    Java Result: 1
    BUILD SUCCESSFUL (total time: 2 seconds)

    i got this error please help me to resolve this
  • Kevin
    I had this issue on linux. Turned out it was simply that the path had a space in it. When this was changed it worked fine.
  • Thunder9751
    how do you execute it with THAT command? like what do you do? where do you type that in (java -classpath)?
  • Chip41092
    Ya, I've read all of these comments and have no clue where to type the java -classpath . XYZ
  • Bancio
    Thanks ... this works for me... I have been seem this type before but I was typeing '.' close to '-classpath' ...
  • puneeth_n
    thanks a lot it was really heplful..
  • Alan
    I just started learning java using the Eclipse IDE. My java for idiots book (ok for dummies) gave me some code that uses swing to produce a message box. There are 2 classes (the book is demonstrating objects).
    It ran fine from within eclipse, but if I tried running it from a command line it wouldn't work. I got the java.lang.NoClassDefFoundError.
    I've searched the net and tried the -classpath . approach and still had no joy.
    I then found a post here
    http://bytes.com/topic/java/answers/15627-java-...
    which refers to the package name as being an issue. I tried following the instructions but still had the issue. Eventually I removed the package definition from both my source files, recompiled and it runs fine. No classpath definition needed.

    Cheers
    Alan
    W00t !
  • irfansiddique
    yes.. a dot .. it means, that it will search for classes bytecode in the current directory !

    you can also tell java to search the current directory with the -classpath parameter

    > java -classpath . Hello

    Problems like this can really discourage java newbies, and that moment was very embarassing :)
  • Setare
    thank you for your help in solving this java error.
  • fredbl
    There are many correct answers here but not really clear to a java noob like me. After pulling my hair out for an hour and reading the answers over and over again it finally struck me.

    The answer for me is this: You should give the path to the directory where your to be executed .class file is, from the directory where you execute it from as you classpath.

    /program/Java/$: Javac hello.java
    /program/Java/$: java hello

    Hello!!

    If i now want to execute it from /program

    /program$: java -classpath ./Java hello

    Hello!!

    or from the root /

    /$: java -classpath ./program/Java hello

    Hello!!

    Hope this makes sense for the ones who did not get it the first ten times like myself.
  • cool0345
    Thanks! this was helpful for me to resolve a pestering issue!
  • kiran
    while intepreting a program in java dont put .java after class name otherwise u will get (thread "main" java.lang.NoClassDefFoundError:)
  • badaniket
    following is my simple awt program it get compile successfully
    but during run it generates following error
    Exception in thread "main" java.lang.NoClassDefFoundError: sampleframe (wrong na
    me: components/sampleframe)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
    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:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
  • badaniket
    pls pls help me
  • Chitre Mangesh
    HI Everyone .
    Following should be checked:=
    Primary check for java -version, if it returns values then installation is proper else re-install
    (Installation path may vary... check yours accordingly .....)

    1)Set JAVA_HOME to C:/Program Files/Java/jdk1.6.0_12
    2) Set CLASSPATH environment to C:/Program Files/Java/jdk1.6.0_12/jre/lib;./;
    (Note :- " ./ " )
    3)Set Path to C:\Program Files\Java\jdk1.6.0_12\bin\
  • Banananana
    If the class you wanna execute is in a package, do this:

    java -classpath . packagename.classname

    Don't forget the spaces after "classpath" and the first "." and note that there aren't spaces after your package name and the following period.
  • josimol
    im getting the error java.lang.NoClassDefFoundError:
    my classpath is c:\jdk1.5\lib.
    give me a help pls
blog comments powered by Disqus