I have a class which have util functions and can be invoked on demand.
Steps to Reproduce :
- TestMainWebApp (this project having Dependency of TestMainImp)
- TestMainImpl ( this project implements TestMainInterface)
- 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);
}
CASE :
- TestMainInterface.jar will be in classpath of TestMainImpl.jar (@compiletime only)
- At RunTime (i wont have TestMainInterface.jar) and I don't invoke the method "checkForTLSErrorRedirection()" . i invoke only test() method.
- But Iam getting , java.lang.NoClassDefFoundError: test/intf/ConfigurationListener
when i create instance itself.
Can you please help to find out the root cause ? How does the java loads Class which is declared within a method ?
NOTE : JavaEETrainingUtil.java for debug purpose
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();
}
}
This is not correct. You should not add the
abstract
keyword to interfaces or their methods. No need forpublic
on interface methods; they are required to be public.This is correct:
Be sure that the
test.intf.ConfigurationListener.class
file appears in your deployment. If it doesn't, the class loader will never find it.