wood burning stoves 2.0*
The moose likes EJB and other Java EE Technologies and the fly likes How to dynamically call a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "How to dynamically call a class" Watch "How to dynamically call a class" New topic
Author

How to dynamically call a class

Seth Ethington
Greenhorn

Joined: Dec 10, 2006
Posts: 4
What I'm doing is I have a website that sends a command property to a front controller servlet and then forwards onto a class. All that I want this class to do is get the command property and dynamically call the appropriate class and its method. For example if the command is "Login" then I want it to call the Login class and the "getLogin" method. I went searching around and here is what I have come up with so far but it throws a Class not found exception. Can anyone please help it is driving me crazy trying to figure this out.

Here is my source:



[ December 10, 2006: Message edited by: Seth Ethington ]
[ December 10, 2006: Message edited by: Seth Ethington ]
pawank gupta
Ranch Hand

Joined: Jun 07, 2006
Posts: 34
why dont you try this via classloader. what you need to do is pass you class suppose "Login" with complete package name like "abc.cde.Login" and then search it in classloader and then create its instance.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9932
    
159

Class.forName((String)request.getParameter("cmd")+"BCO").newInstance()


1) Make sure the correct class name(including the case and the fully qualified name) is being passed through the "cmd" parameter. Also make sure that you dont have leading and trailing spaces in that value.

2) Also make sure that this class, that you are trying to load, is in the classpath of the application.


[My Blog] [JavaRanch Journal]
Seth Ethington
Greenhorn

Joined: Dec 10, 2006
Posts: 4
Ok now I'm trying this. I decided to put in a static fully qualified name so I could trouble shoot. I'm now getting a Class type cast exception.

Any ideas
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9932
    
159

Does your com.HomeTownBank.bco.BranchBCO implement the ApplicationControllerInterface? Also post the entire exception stacktrace.
Seth Ethington
Greenhorn

Joined: Dec 10, 2006
Posts: 4
Yes I am implementing the ApplicaitonControllerInterface in the Applicaiton Controller.

Here is my stack:

java.lang.ClassCastException: com.HomeTownBank.bco.BranchBCO
at com.HomeTownBank.HTBApplicationController2.requestBusinessControlObject(HTBApplicationController2.java:21)
at com.HomeTownBank.HTBFrontController.doGet(HTBFrontController.java:46)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Dec 11, 2006 12:35:08 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet HTBFrontController threw exception
java.lang.ClassCastException: com.HomeTownBank.bco.BranchBCO
at com.HomeTownBank.HTBApplicationController2.requestBusinessControlObject(HTBApplicationController2.java:21)
at com.HomeTownBank.HTBFrontController.doGet(HTBFrontController.java:46)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Dec 11, 2006 12:39:16 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet HTBFrontController threw exception
java.lang.ClassCastException: com.HomeTownBank.bco.BranchBCO
at com.HomeTownBank.HTBApplicationController2.requestBusinessControlObject(HTBApplicationController2.java:21)
at com.HomeTownBank.HTBFrontController.doGet(HTBFrontController.java:46)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Dec 11, 2006 12:54:50 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet HTBFrontController threw exception
java.lang.ClassCastException: com.HomeTownBank.bco.BranchBCO
at com.HomeTownBank.HTBApplicationController2.requestBusinessControlObject(HTBApplicationController2.java:21)
at com.HomeTownBank.HTBFrontController.doGet(HTBFrontController.java:53)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Pratheep Nair
Greenhorn

Joined: Sep 20, 2005
Posts: 14
If BranchBCO does not implement the ApplicationControllerInterface interface, you will get a ClassCastexception. Looking at the stack trace I don't what you are calling at line no....
Seth Ethington
Greenhorn

Joined: Dec 10, 2006
Posts: 4
Thanks for your last post your were right. I had 2 problems. Both of which was in my BranchBCO file. I didn't say that the class implements the interface and second I had my method set up as public static and my interface was only public. This is why I was getting the type cast error.

Thanks for trying to help though
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to dynamically call a class
 
Similar Threads
problem implementing iReports with servlets
Problem executing SQL in SERVLET.
MVC2 outside servlet for testing
J2ME connecting database in Server using Servlet
Looking For Connection Bean For Connection Pool