类加载器如何加载在方法内部声明的扩展类的实例? (java.lang.NoClassDefFoundError)

时间:2019-03-18 11:15:52

标签: java noclassdeffounderror contextclassloader

我有一个具有util函数的类,可以按需调用。

复制步骤:

  1. TestMainWebApp(该项目具有TestMainImp的依赖项)
  2. TestMainImpl(此项目实现TestMainInterface)
  3. TestMainInterface

TestMainWebApp> TestMainServlet.java

package com.main;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.test.util.Util;
/**
 * Servlet implementation class TestMainServlet
 */
@WebServlet("/TestMainServlet")
public class TestMainServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   
         System.out.println("\nCurrent ClassLoader chain: "+JavaEETrainingUtil.getCurrentClassloaderDetail());
         Util prov = new Util();
        // prov.test();
        }
}

TestMainImpl> Util.java和TLSErrorRedirectListener.java

package com.test.util;
public class Util {
     private final static String CLAZZ = Util.class.getName();
    static {
            System.out.println("Classloading of "+CLAZZ+" in progress..."+JavaEETrainingUtil.getCurrentClassloaderDetail());

     }
        public boolean checkForTLSErrorRedirection(boolean b) {                         
            test.intf.ConfigurationListener listener = new com.listener.TLSErrorRedirectListener();   
            listener.valueChanged("test", "test");
            return true;

        }    

public void test() {  
        System.out.println(" test util");  
    }

}

TLSErrorRedirectionListener.java

package com.listener;
import com.test.util.JavaEETrainingUtil;
public class TLSErrorRedirectListener implements test.intf.ConfigurationListener {
     final static String CLAZZ = TLSErrorRedirectListener.class.getName();
     static {
            System.out.println("Classloading of "+CLAZZ+" in progress..."+JavaEETrainingUtil.getCurrentClassloaderDetail());
     }
    public void valueChanged(String key, String value) {
        switch(key){
        case "test1": 
        default : break;
        }
    }
}

TestMainInterface> ConfigurationListener.java

package test.intf;
public abstract interface ConfigurationListener
{
  public abstract void valueChanged(String paramString1, String paramString2);
}

案例:

  1. TestMainInterface.jar将位于TestMainImpl.jar的类路径中(仅@compiletime)
  2. 在运行时(我没有TestMainInterface.jar),并且我不调用方法“ checkForTLSErrorRedirection()”。我只调用test()方法。
  3. 但是我正在获取java.lang.NoClassDefFoundError:test / intf / ConfigurationListener
    当我创建实例本身时。

能否请您帮助找出根本原因? Java如何加载方法中声明的Class?

注意:用于调试目的的JavaEETrainingUtil.java

package noclassdef.example1;
import java.util.Stack;
import java.lang.ClassLoader;

/**
 * JavaEETrainingUtil
 * @author Pierre-Hugues Charbonneau
 *
 */
public class JavaEETrainingUtil {

        /**
         * getCurrentClassloaderDetail
         * @return
         */
        public static String getCurrentClassloaderDetail() {

               StringBuffer classLoaderDetail = new StringBuffer();       
               Stack<ClassLoader> classLoaderStack = new Stack<ClassLoader>();

               ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();

               classLoaderDetail.append("\n-----------------------------------------------------------------\n");

               // Build a Stack of the current ClassLoader chain
               while (currentClassLoader != null) {

                       classLoaderStack.push(currentClassLoader);

                       currentClassLoader = currentClassLoader.getParent();
               }

               // Print ClassLoader parent chain
               while(classLoaderStack.size() > 0) {

                       ClassLoader classLoader = classLoaderStack.pop();

                       // Print current                     
                       classLoaderDetail.append(classLoader);

                       if (classLoaderStack.size() > 0) {
                              classLoaderDetail.append("\n--- delegation ---\n");           } else {
                              classLoaderDetail.append(" **Current ClassLoader**");
                       }
               }
               classLoaderDetail.append("\n-----\n");
               return classLoaderDetail.toString();
        }
}

1 个答案:

答案 0 :(得分:0)

这是不正确的。您不应将abstract关键字添加到接口或其方法中。不需要接口方法上的public;他们必须公开。

这是正确的:

package test.intf;
public interface ConfigurationListener {
  void valueChanged(String paramString1, String paramString2);
}

确保test.intf.ConfigurationListener.class文件出现在您的部署中。否则,类加载器将永远找不到它。