Android Error:java.lang.NoClassDefFoundError

Source: Internet
Author: User

Reprint: HTTP://WWW.TUICOOL.COM/ARTICLES/IVBVN2

Originalhttp://deepinmind.iteye.com/blog/2078286

When an exception occurs in the static initialization block, the JVM throws a Java.lang.ExceptionInInitializerError exception. If you know the static variables in Java, you will know that they are initialized when the class is loaded. If an exception occurs during initialization of this static variable, a Java.lang.ExceptionInInitializerError exception is thrown. Any exception can cause this, for example, Java.lang.ArrayIndexOutOfBound or java.lang.NullPointerException. Java developers are often stunned by this error and feel that they do not define any static initialization blocks, and why they throw exceptionininitializererror exceptions; in fact, Java defaults to the initialization of static variables in a default static initialization block. , and then initialize them in the order they were declared in the source file. For example, if the variable ABC declaration is used in the first row, in the second row, and in the third row, then the code in the second line throws a NullPointerException exception, which is encapsulated in a Exceptionininitializererror exception, if this code is executed in the main thread, you will see an error message in the console or in the log file: "Exception in Thread" main " Java.lang.ExceptionInInitializerError ". In a large system with a large number of log files, such errors are easily overlooked and the programmer gets a Java.lang.NoClassDefFoundError exception. Unfortunately, this error occurs only when someone else uses the class, because Exceptionininitializererror causes the class to fail to load. Because the class failed to load, the JVM throws Noclassdeffounderror. Sometimes it's misleading for Java developers to check the classpath, path, and Java.library.path to see if they're missing the class, but they can't find any problems, which is confusing. If you're analyzing Noclassdeffounderror's reasons, you'd better look at your log file for Exceptionininitializererror, and then consider whether you want to check for classpath. In this article we will see a piece of code that throws an exception during static initializationthereby causing "Exception in thread" main "Java.lang.ExceptionInInitializerError". In a later section, we will see how to solve this problem.
Exception in thread "main" Java.lang.ExceptionInInitializerError reason
As with other errors or exceptions, when you see this line of information, you know that this is a Exceptionininitializererror exception, which is caused by the failure of the static block initialization process during class loading. Since it appears in the main thread responsible for starting the program, you'd better start parsing from the main class, where the main class is the one you specified in the command-line arguments, or the class that you declared the public static void main (String args[]) method. If you take a closer look at the full stack trace, you don't have to do anything because the JVM has printed the class name, which is the class that triggered the exceptionininitializererror. Exceptionininitializererror is a subclass of Linkageerror, which means that this exception causes your class to fail to load into the JVM's memory. Now let's take a look at this example program, which throws the following exception when it executes:

0 at        Java.util.ArrayList.rangeCheck (Arraylist.java:635) at        Java.util.ArrayList.get (Arraylist.java: 411) at Staticinitiazerdemo. <clinit> (staticinitiazerdemo.java:15)   

Looking at the stack trace information, you know the real exception is Java.lang.IndexOutOfBoundsException, which is thrown out in the second line of Staticinitiazerdemo. This is because you called the ArrayList Get () method and passed in position 0, and the ArrayList size is also 0 (index:0, size:0). After seeing this message you know that when we want to remove the first creditcard from the list, the list is empty.

Import java.util.ArrayList;Import java.util.List;/** * Java program to understand and solve exceptioninitializererror, which comes * when static initializer blocks throws Unchecked exception during class loading * and initialization. * *@author Javin Paul * *PublicClassstaticinitializerdemo{ PrivateStaticFinal list<creditcard> cards =New Arraylist<creditcard> (); Privatestatic CreditCard Prefferdcard = Cards.get (0);1st card is default PublicStaticBoolean Isvisa ="VISA". Equalsignorecase (Prefferdcard.getnetwork ()); PublicStaticvoid Main (String args[]) { Makepayment (Prefferdcard);} PublicStaticvoid Makepayment (CreditCard cc) {  if (Isvisa) {   Offer 5% discount }  Deduct payment}}ClassCreditCard { PrivateLong Card_number;Digit card number Privateint CVV;3 digit CVV number Privateint expirymonth; Privateint expiryyear; Private String Bank; Private String Network; Public CreditCard (Long Card_number,int CVV,int Expirymonth,int Expiryyear, string bank, string network) {  Super ();  This.card_number = Card_number;  THIS.CVV = CVV;  This.expirymonth = Expirymonth;  This.expiryyear = Expiryyear;  This.bank = Bank;  This.network = network;} /***@return The Card_number*/ PublicFinalLong Getcard_number () {  return card_number;} /***@return The CVV*/ PublicFinalint Getcvv () {  return CVV;} /***@return The Expirymonth*/ PublicFinalint Getexpirymonth () {  return expirymonth;} /***@return The Expiryyear*/ PublicFinalint Getexpiryyear () {  return expiryyear;}  /** *  @return the bank< Span class= "indent" > */ public final String getbank () {  return bank;}  /** *  @return the network  */ public final String Getnetwork () {  return network;}}  

Output:

0 at    Java.util.ArrayList.rangeCheck (Unknown source) at    Java.util.ArrayList.get (Unknown source)    at Staticinitializerdemo.  <clinit> (staticinitializerdemo.java:15) 

Here is the class structure of all the error classes in Java. You can see that Exceptionininitializererror is inherited from Linkageerror. It should also be known that, like RuntimeException, error is not checked exception, the compiler is not to check for the corresponding exception handling code.

how to resolve exception in thread "main" Java.lang.ExceptionInInitializerError
Here are a few things to keep in mind:
1. "Exception in Thread" main "java.lang.ExceptionInInitializerError" means that the exception appears in the main thread, And is a subclass of Linkageerror Java.lang.ExceptionInInitializerError, which is thrown when the JVM class fails to load, because there is a static initialization code such as indexoutofboundsexception or null Pointerexception such a runtimeexception.
2. Remember that the JVM places the initialization of all static variables into a static initialization block in the order in which they appear in the source file. Therefore, do not assume that this exception does not occur if you do not see the static initial block. In fact, you have to make sure that the static variables are in the correct order, for example, if a variable is initialized with another variable, you have to make sure that the variable is initialized before.
3. If another code wants to use this class, it throws a Exceptionininitializererror exception, which in turn causes classnotfoundexception or Noclassdeffounderror. Why? Because this class fails to load, it is not loaded into the JVM's memory. So if you're dealing with exceptions such as a class that doesn't exist, let's look at the exception in your log file.
4. Remember that static initialization blocks throw runtimeexception instead of checked exceptions, which require a corresponding catch block for processing.
This is everything about exception in thread "main" java.lang.ExceptionInInitializerError. You've learned how to track this type of problem and find the culprit that throws it. It is necessary to remember that one of the side effects of this anomaly is noclassdeffounderror, The Java program throws this exception in a location that may be far from Java.lang.ExceptionInInitializerError, depending on when your client code refers to this class. Therefore, before looking at the Classpath to resolve noclassdeffounderror exceptions, it is a good idea to check the log for Exceptionininitializererror.

Android Error: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.