exception in thread 'main' java.lang.NoClassDefFoundError:

后端 未结 23 3351
心在旅途
心在旅途 2020-11-28 06:27

The following program is throwing error:

public class HelloWorld {
    public static void main(String args[]) {
        System.out.println(\"Hello World!\");         


        
相关标签:
23条回答
  • 2020-11-28 06:32

    I spent four hours trying various permutations & search suggestions.

    At last, found this post that worked but not the best solution to change the original code to test it.

    1. Tried changing CLASSPATH, to include class libraries, set classpath=JDKbin;JDKlib;JREbin;JRElib;myClassLib;.
    2. Changed the current directory to parent directory(package folder) and tired java <packageName>.<className> also tried the java ..\<packageName>.<className>

    Neither worked.

    However first response alone worked. Thank you so much Abhi!!!

    "I found one another common reason. If you create the java file inside a package using IDE like eclipse, you will find the package name on the top of your java file like "package pkgName". If you try to run this file from command prompt, you will get the NoClassDefFoundError error. Remove the package name from the java file and use the commands in the command prompt. Wasted 3 hours for this. -- Abhi"

    Curious if there is any other way to make it work without having to change the original code? Thank you!

    0 讨论(0)
  • 2020-11-28 06:33

    The javadoc of NoClassDefFounError itself would be a good start (here), and then I'll suggest you clean and rebuild your project.

    0 讨论(0)
  • 2020-11-28 06:34

    I finally found this as a bug with Apache Netbeans editor:

    Below steps will remove the error:

    1. Rename the filename & class to Abc
    2. Close the editor
    3. Reopen the editor
    4. Rename the filename & class, from Abc, back to the previous name
    5. Now debug project (Ctrl+F5) works fine

    Hope that helps, if you are using new Apache Netbeans (not old Netbeans)

    0 讨论(0)
  • 2020-11-28 06:36

    Run it like this:

    java -jar HelloWorld.jar
    
    0 讨论(0)
  • 2020-11-28 06:38

    Java-File:

    package com.beans;
    
    public class Flower{
     .......
    }
    
    packages :=> com.beans,
    java class Name:=> Flower.java,
    Folder structure (assuming):=> C:\com\beans\Flower.*(both .java/.class exist here)
    

    then there are two ways of executing it:

    1. Goto top Folder (here its C:\>),
         then : C:> java com.beans.Flower 
    2. Executing from innermost folder "beans" here (C:\com\beans:>),
         then: C:\com\beans:> java -cp ./../.. com.beans.Flower
    
    0 讨论(0)
  • 2020-11-28 06:39
    Exception in thread "main" java.lang.NoClassDefFoundError  
    

    One of the places java tries to find your .class file is your current directory. So if your .class file is in C:\java, you should change your current directory to that.

    To change your directory, type the following command at the prompt and press Enter:

    cd c:\java  
    

    This . tells java that your classpath is your local directory.

    Executing your program using this command should correct the problem:
    java -classpath . HelloWorld  
    
    0 讨论(0)
提交回复
热议问题