aspose file tools*
The moose likes Java in General and the fly likes How to invoke a method from a class which is loaded Dynamically??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to invoke a method from a class which is loaded Dynamically???" Watch "How to invoke a method from a class which is loaded Dynamically???" New topic
Author

How to invoke a method from a class which is loaded Dynamically???

Murali Obla
Ranch Hand

Joined: Nov 18, 2001
Posts: 40
Hello folks,

I have loaded a class dynamically and I have retrieved the methods under that class.I was trying to invoke those mehtods but it's causing exceptions!!!.
Here is the part of my code
[CODE]
Class obj=Class.forName(cTName);
System.out.println("Class Name "+cName);
Method[] list= obj.getDeclaredMethods();
for(int i=0;i<3;i++)
{
System.out.println("Method "+list[i].getName());
System.out.println("Return Type is "+list[i].getReturnType());
System.out.println("Parameters Required "+list[i].getParameterTypes());
Object methodInvoke = list[i].invoke(obj,null);

}
System.out.println();
[CODE]
I can't understand what getParameterTypes is returning
Folks,Please help me how to invoke the methods???
Thanks in Advance,
Murali


"Excellence is never an accident; it is always the result of high intention, sincere effort, intelligent direction, skillful execution, and the vision to see obstacles as opportunities"
sandy gupta
Ranch Hand

Joined: Jan 30, 2001
Posts: 228
Use the Mthod class in the reflection API.....u have to know the name of the method in order to call it using the same.....
HTH
Sahil


Adios
Murali Obla
Ranch Hand

Joined: Nov 18, 2001
Posts: 40
Hi Sahil,

Can u give me some sample code....
Regards,
Murali
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
When you invoke a method you have to give it the input parameters that the signature requires. When you used list[i].invoke(obj,null); passing the null was the same as saying "invoke the version of this method that takes no parameters. Well - there might not BE such a method. You use the getParameterTypes to create an array containing objects of the correct types to match what the input parameters for that method are.
So you should pass it an array containing objects of the types that you "created" when you called list[i].getParameterTypes());.
So the call should be
Object methodInvoke = list[i].invoke(obj,list[i].getParameterTypes());


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to invoke a method from a class which is loaded Dynamically???