Jar Pack Conflict and Java.lang.NoSuchMethodError exception solution

Source: Internet
Author: User
problem

In the coding process, the problem of jar bundle conflict is often encountered. The performance characteristics of the problem are generally thrown java.lang.NoSuchMethodError anomalies. So, today we'll talk about how to solve such problems. Analysis

This problem is related to the Java class loading mechanism. When the JVM loads the class, it uses the "overall responsible delegation mechanism", which is related to the "delegation mechanism". It means that the parent loader is first searched for the target class, and the target class is found and loaded from its own classpath only if it cannot be found.

However, if more than one version of the class package is placed under the classpath, such as Commons-lang 2. X.jar and Commons-lang3.x.jar are all in the classpath, the code uses a method of the Commons-lang3.x class, which does not exist in commons-lang2.x, and the JVM happens to load classes from the Commons-lang 2.x.jar The class is loaded, the runtime throws a nosuchmethoderror error. Problem Troubleshooting

This sort of problem is tricky to troubleshoot, especially when it comes to Web applications where there are more system catalogs that can be used as classpath, especially when there are large classes of packages: You don't know exactly which class package the JVM is loading from.

The following provides a srcadd.jsp program, you put it under the root path of the Web application, and you can see from which class the JVM loads the specified class (where classname is the full name of the class classname parameter is not required. class suffix, port number is the port number for the Web project, here is 80):

    

Srcadd.jsp source code is as follows:

<% @page contenttype= "text/html;

  CHARSET=GBK "%> <% @page import=" java.security.*,java.net.*,java.io.* "%> <%! public static URL getclasslocation (final Class cls) {if (CLS = = null) throw new illegalargumentexception ("Null Input:
    CLS ");
    URL result = null;
    Final String Clsasresource = Cls.getname (). replace ('. ', '/'). Concat (". Class");
    Final Protectiondomain PD = Cls.getprotectiondomain ();
    Java.lang.Class contract does not specify if ' PD ' can ever to be null; It isn't the case for Sun's implementations, but guard against NULL//Just in case:if (PD!= null) {F
      Inal Codesource cs = Pd.getcodesource ();
      ' CS ' can is null depending on the ClassLoader behavior:if (CS!= null) result = Cs.getlocation ();  if (result!= null) {//Convert a code source location to a full class file location//For some common Cases:if ("File". Equals (Result.getprotocol ())) {try {if (Result. Toexternalform (). EndsWith (". Jar") | |
                               Result.toexternalform (). EndsWith (". zip") result = new URL ("Jar:". Concat (Result.toexternalform ())
            . Concat ("!/"). Concat (Clsasresource));
          else if (new File (Result.getfile ()). Isdirectory ()) result = new URL (result, clsasresource);  catch (Malformedurlexception ignore) {}}} if (result = = NULL) {//Try to find ' CLS ' definition as a resource; This is not//document. D to being legal, but Sun ' s implementations seem to//allow this:final ClassLoader clsloader = Cls.getclassloa
      Der ();
          result = Clsloader!= null?
    Clsloader.getresource (Clsasresource): Classloader.getsystemresource (Clsasresource);
  return result;
  %> 

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.