The difference between java.lang.ClassNotFoundException and Java.lang.NoClassDefFoundError

Source: Internet
Author: User

Has not been aware of this problem, the first two days of chance coincidence on the Internet to check a bit, and then their own test verification. Although the online saying a lot, but about Noclassdeffounderror did not give a sample, so has been unable to understand, simply self-verified a bit, Harvest still a lot.

ClassNotFoundException

ClassNotFoundException This error, more common or understanding.

Reason: The specified class could not be found.

A common scenario is:

1 The specified class was not found when calling the Forname method of class

2 ClassLoader in the Findsystemclass () method, the specified class was not found

3 ClassLoader in the LoadClass () method, the specified class was not found

Developers will usually have such a way to use, similar to the JDBC loading driver!

1 package test321; 2  3 public class Test {4 public  static void Main (string[] args) {5   try {6    class.forname ("Test321.hello" ); 7   } catch (ClassNotFoundException e) {8    e.printstacktrace (); 9   }10  }11}

At this point, the program will look in the current directory for the specified location Test321.hello this class.

And this class can also be performed normally.

However, we modify the loaded class name so that the specified class is obviously not found.

1 package test321; 2  3 public class Test {4 public  static void Main (string[] args) {5   try {6    class.forname ("Test321.hello1 "); 7   } catch (ClassNotFoundException e) {8    e.printstacktrace (); 9   }10  }11}

This will be an error!

Error! Java.lang.ClassNotFoundException:test321.hello1 at Java.net.urlclassloader$1.run (urlclassloader.java:366) at Java.net.urlclassloader$1.run (urlclassloader.java:355) at java.security.AccessController.doPrivileged (Native Method) at Java.net.URLClassLoader.findClass (urlclassloader.java:354) at Java.lang.ClassLoader.loadClass ( classloader.java:425) at Sun.misc.launcher$appclassloader.loadclass (launcher.java:308) at Java.lang.ClassLoader.loadClass (classloader.java:358) at JAVA.LANG.CLASS.FORNAME0 (Native Method) at Java.lang.Class.forName (class.java:190) at Test321.test.main (Test.java:6)

The reason is that the class file corresponding to the specified string cannot be found.

Noclassdeffounderror

This is more wonderful, looking for other information is that through the compilation, but when used, such as new when the error.

By finding the data, the following scenarios are collected:

Class 1-dependent classes or jars do not exist

Class 2 file exists, but exists in a different domain

3 case, Javac compile time is ignored size, it is likely that you compiled the class file is not the same as you want! This does not do validation.

  

For the 2nd above, a validation of the package name is done:

Another situation is because you passed the compilation, but this class is a package name, so at compile time need to specify CLASSPATH, in use when you need to add the package name.

Here's a small example!

In the absence of a package name, let's look at what the normal situation looks like.

1 public class Test {2 public  static void Main (string[] args) {3   System.out.println ("test"); 4  }5}

And if the class contains the package name, then follow the above method of compiling, use will be error!

1 Package ccc;2 3 public class Test {4 public  static void Main (string[] args) {5   System.out.println ("test"); 6  }7}

Obviously, the information in the error message indicates the package! So what do we do?

At compile time, add "-D. "This allows you to add the current directory to the Classpath.

When in use, add the package name to it!

That is, the class containing the name of the package, compile, you need to specify the path of Classpath, and use the time to specify the full path of the package name.

The difference between java.lang.ClassNotFoundException and Java.lang.NoClassDefFoundError

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.