The solution

  1. Check packet conflicts using POM files
  2. Install Maven Helper for IDEA
  3. Locate the POM file where the WAR package is compiled (defined by our framework in the Deploy module)



4. In the search box, enter the search content and right-click to view the photo selection box

  • Jump To Source
  • Exclude

For example, if you click Exclude, you can see that the dependency is excluded from the POM file

<dependency>
    <groupId>cn.com.xxx</groupId>
    <artifactId>framework-conf-client</artifactId>
    <version>${xqy.framework.version}</version>
    <exclusions>
        <exclusion>
            <artifactId>slf4j-log4j12</artifactId>
            <groupId>org.slf4j</groupId>
        </exclusion>
    </exclusions>
</dependency>
Copy the code

After removing dependencies, submit code, repackage, deploy a dragon, and start up smoothly

thinking

Package conflict resolution is simple. Maven plug-in can accurately find dependencies and Exclude them. However, there are no problems in local development and test environment, but there are problems in pre-release environment, so the cause of business logic code is excluded.

  • JDK version
  • Tomcat version
  • Class loading mechanism
  • Third-party jars depend on each other

Class load mechanism can be referenced: class load mechanism >>> Java uses parent delegate loading mechanism, by viewing the ClassLoader class, you can understand this.

Once the Class is successfully loaded, it is put into memory, where the Class instance object is stored. The JVM can be started with commands such as -verbose or -xx :+TraceClassLoading

protectedClass<? > loadClass(String name,boolean resolve)
    throws ClassNotFoundException
{
    synchronized (getClassLoadingLock(name)) {
        // First, check if the class has already been loaded
        // First, check to see if the class is already loadedClass<? > c = findLoadedClass(name);if (c == null) {
            // If not loaded
            long t0 = System.nanoTime();
            try {
                if(parent ! =null) {
                    // Find the parent loader
                    c = parent.loadClass(name, false);
                } else {
                    // If the parent loader does not exist, delegate the load to the starting class loaderc = findBootstrapClassOrNull(name); }}catch (ClassNotFoundException e) {
                // ClassNotFoundException thrown if class not found
                // from the non-null parent class loader
            }
            if (c == null) {
                // If still not found, then invoke findClass in order
                // to find the class.
                // If it still cannot be loaded, it will try to load itself
                long t1 = System.nanoTime();
                c = findClass(name);
                // this is the defining class loader; record the statssun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0); sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1); sun.misc.PerfCounter.getFindClasses().increment(); }}if (resolve) {
            resolveClass(c);
        }
        returnc; }}Copy the code

Class loading order

From the code, we know that if a class with a certain name is loaded, the class loader will not reload, so the root cause of our problem can be as follows: First load org. Org slf4j packs. Slf4j. Impl. StaticLoggerBinder, of the same name ch. Qos. Logback StaticLoggerBinder class under the package has not been loaded

Conclusion:

JVM startup errors are usually related to dependency package conflicts

  • Java. Lang. ClassNotFoundException: type conversion error
  • Java. Lang. NoSuchMethodError: can’t find the specific method, if there are two packages with the same but different versions, such as: 1.2-1.1 and XXX XXX packages exist at the same time, to load the 1.1 version of the first class, but in version 1.2 provides a new method, lead to tip can’t find the specific method
  • java.langNoClassDefFoundError

In advance to prevent

  • Use tools to check for dependency conflicts

Conflict detection plugin: Maven-enforcer-plugin references a new third-party dependency (toolkit or framework package). Check the conflict dependency by using the Maven plugin and Exclude it beforehand

  • Unified Server Version

In the test phase, prepare the same server as the production environment, conduct the test in advance, avoid the dependency conflict WAR package upload to the production environment, for example, we have a UAT server, configure the same as the production environment, test in advance, expose risks and solve problems