How to solve java.lang.NoClassDefFoundError?

前端 未结 27 1497
暗喜
暗喜 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: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]
    

提交回复
热议问题