Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help me out with dynamic class loader  RSS feed

Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all
I have trouble with Class.forName() to load class dynamically. This is a coed used in dispatch service.
My code is like
Class c = Class.forName(methodList.get(myMethod));
methodList is an instance of class that maintain the list of key/value. for example myMethod (key) TestMethod(value)
Can anyone gibe me some hint what's the possible reason that it cannot find the dynamic class?
Thank you so much!

The error is:
java.lang.ClassNotFoundException: TestMethod
at org.apache.tomcat.loader.AdaptiveClassLoader.loadClass(
at java.lang.ClassLoader.loadClass(
at java.lang.ClassLoader.loadClassInternal(
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(
at pointclickweb.servlet.Servlet.service(
at javax.servlet.http.HttpServlet.service(
at org.apache.tomcat.core.ServletWrapper.doService(
at org.apache.tomcat.core.Handler.service(
at org.apache.tomcat.core.ServletWrapper.service(
at org.apache.tomcat.core.ContextManager.internalService(
at org.apache.tomcat.core.ContextManager.service(
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(
at org.apache.tomcat.service.TcpWorkerThread.runIt(
at org.apache.tomcat.util.ThreadPool$
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try to find out what is the current directory for the class loader
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
with ear file, war file approach there are different class loaders. For ex. If you deploy an application using ear file on an app server, then there 3 different class loaders constructed internally.
1. system class loader
2. EJB class loader
3. Web class loader
The hierarchy of these class loaders is as follows.
system class loader<-EJB class loader<-web class loader
"<-" inheritence
A<-B - B inherits from A.
NOTE. a class loader can only access classed loaded by parent or itself. It CANNOT access classes loaded by any of its children class loader.
answer to your specific question...
"Could it be that you are calling a class loaded by child"
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!