How to solve java.lang.NoClassDefFoundError?

前端 未结 27 1492
暗喜
暗喜 2020-11-21 06:04

I\'ve tried both the example in Oracle\'s Java Tutorials. They both compile fine, but at run-time, both come up with this error:

Exception in thread \"main\"         


        
相关标签:
27条回答
  • 2020-11-21 06:40

    After you compile your code, you end up with .class files for each class in your program. These binary files are the bytecode that Java interprets to execute your program. The NoClassDefFoundError indicates that the classloader (in this case java.net.URLClassLoader), which is responsible for dynamically loading classes, cannot find the .class file for the class that you're trying to use.

    Your code wouldn't compile if the required classes weren't present (unless classes are loaded with reflection), so usually this exception means that your classpath doesn't include the required classes. Remember that the classloader (specifically java.net.URLClassLoader) will look for classes in package a.b.c in folder a/b/c/ in each entry in your classpath. NoClassDefFoundError can also indicate that you're missing a transitive dependency of a .jar file that you've compiled against and you're trying to use.

    For example, if you had a class com.example.Foo, after compiling you would have a class file Foo.class. Say for example your working directory is .../project/. That class file must be placed in .../project/com/example, and you would set your classpath to .../project/.

    Side note: I would recommend taking advantage of the amazing tooling that exists for Java and JVM languages. Modern IDE's like Eclipse and IDEA and build management tools like Maven or Gradle will help you not have to worry about classpaths (as much) and focus on the code! That said, this link explains how to set the classpath when you execute on the command line.

    0 讨论(0)
  • 2020-11-21 06:41

    if you got one of these error while compiling and running:

    * NoClassDefFoundError
    
    * Error: Could not find or load main class hello
    
    * Exception in thread "main" java.lang.NoClassDefFoundError:javaTest/test/hello 
    (wrong name: test/hello)
            at java.lang.ClassLoader.defineClass1(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.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 sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
    

    -------------------------- SOLUTIION -----------------------

    the problem is mostly in packages organization. You should arrange your classes in folders properly regarding to the package classifications in your source code.

    On Compiling process use this command:
    
    javac -d . [FileName.java]
    
    To Run the class please use this command:
    
    java [Package].[ClassName]
    
    0 讨论(0)
  • 2020-11-21 06:43

    My two cents in this chain:

    Ensure that the classpath contains full paths (/home/user/lib/some_lib.jar instead of ~/lib/some_lib.jar) otherwise you can still face NoClassDefFoundError error.

    0 讨论(0)
  • 2020-11-21 06:45

    It happened to me in Android Studio.

    The solution that worked for me: just restart the studio.

    0 讨论(0)
  • 2020-11-21 06:46

    No Class Definition Exception occurs when the intended class is not found in the Class Path. At Compile Time Class : Class was generated from Java Compiler, But Somehow at Run Time the Dependent Class is not found.

    Lets go through one Simple Example :

    public class ClassA{
    public static void main(String args[]){
         //Some gibberish Code...
         String text = ClassB.getString();
         System.out.println("Text is :" + text);
    }
    

    }

    public class ClassB{
        public static String getString(){
          return "Testing Some Exception";
     }
    }
    

    Now Lets assume that the above two Java Source Code are placed in some Folder let say "NoClassDefinationFoundExceptionDemo"

    Now open a shell(Assuming Java is already being setup correctly)

    1. Go to Folder "NoClassDefinationFoundExceptionDemo"
    2. Compile Java Source Files javac ClassB javac ClassA
    3. Both Files are Compiled Sucessfully and generated Class files in the same Folder as ClassA.class and ClassB.class
    4. Now Since we are overiding ClassPath to current working directory therefore we execute the following command java -cp . ClassA and It worked Successfully and you will see the Output in the screen
    5. Now Lets say, You removed ClassB.class file from Present Directory. and now you execute the command again. java -cp . ClassA Now it will greet you with NoClassDefFoundException. as ClassB which is a dependency for ClassA is not found in the classpath(i.e present working directory).
    0 讨论(0)
  • 2020-11-21 06:46

    For my project, what solved the issue was that Chrome browser and chromedriver were not compatibles. I had a very old version of the driver that could not even open the browser. I just downloaded the latest version of both and problem solved. How did I discovered the issue? Because I ran my project using the Selenium native firefox driver with an old version of FF inculded with my application, I realized then that the problem was incompatibility of between browser and driver.

    Hope this can help anyone with a similiar issue as mine, that generated this same Error Message.

    0 讨论(0)
提交回复
热议问题