Looking for a date or love? Click here to find one for free!!
Find Singapore Apartment and Room Rentals here
Invoking a method that accepts array parameters using java reflection is a little bit tricky. The following code shows how to do it the right way and the possible errors you may see if you get it wrong.
–Class with a method that accepts an array —
public class Dao {
public void Method2(String[] params){
//do something
}
}
— Test Class With CORRECT CODE–
public class Test {
public static void main(String[] args) throws Exception{
Class classToCall = Class.forName(“Dao”);
String[] argu ={“1″,”2”};
Method methodToExecute = classToCall.getDeclaredMethod(“Method2”, new Class[]{String[].class});
methodToExecute.invoke(classToCall.newInstance(), new Object[]{argu});
}
}
— Test Class WITH CODE that gives java.lang.IllegalArgumentException: wrong number of arguments—
public class Test {
public static void main(String[] args) throws Exception{
Class classToCall = Class.forName(“Dao”);
String[] argu ={“1″,”2”};
Method methodToExecute = classToCall.getDeclaredMethod(“Method2”, new Class[]{String[].class});
methodToExecute.invoke(classToCall.newInstance(), argu);
}
}
— Test Class WITH CODE that gives java.lang.IllegalArgumentException: argument type mismatch—
public class Test {
public static void main(String[] args) throws Exception{
Class classToCall = Class.forName(“Dao”);
String[] argu ={“1”};
Method methodToExecute = classToCall.getDeclaredMethod(“Method2”, new Class[]{String[].class});
methodToExecute.invoke(classToCall.newInstance(), argu);
}
}
Wonderful post! This was perfect in solving exactly this problem at work today.
This was a huge help to me. Thanks!
but what about byte[] parameter ?
it throws java.lang.IllegalArgumentException: argument type mismatch.
any solution?
Many thanks, this helped me.
This was really a great taught to post such a blocker.
Thanks alot
Thanks, super useful !!
Perfect examples! Believe me, the Oracle docs for this matter are way more complex by way less useful as your examples! Good job!
Please remove the email from my name, for my last post, if possible 🙂
Entered it twice by mistake.
Super helpful… I was struggling with this and then Google turned up your posting about it. I was using the following set-up, which looks very naive compared to the proper solution:
// init:
Class clas = Class.forName(“foo”);
Method m = clas.getMethod(“bar”, String[].class);
// invocation:
String[] param = somethingOrOther();
return m.invoke(null, param); // null because foo.bar is a static
I don’t understand what the extra step of creating a Class array in the init and creating an Object array in the invocation is providing, I guess that’s something else to Google. I can’t help feeling that there is something not quite natural about Array semantics in Java, maybe it’s partially legacy stuff from C.
But most of all Thanks, you saved me a lot of time 🙂
Great Soulution… Thank you….
Oh boy! sure you save lot of our time.
I always use new Class[] {} & new Object[] {}, but started directly putting the classes / objects to simplify coding 🙂
But, Gotcha.. i got into this.
Thanks much!
excellent, the trick was to think about an array as an *Object 🙂
Thanks, this really helped.
How would I be able to figure this on my own?
Sir, i m fresher student i m developing a program in which by passing any class name with package name. we get all the information of the class and also invoke any method as per user required. almost i have completed my task but still i have facing problem with the class array type reference as parameter.
porblem:-
,let’s us suppose a method having parameter one class object array,int ,long. Example:- public void show(Class obj[], int num, long num1).
How can invoke this type method.
Help me please ……!!
Thanks!
Have you ever considered about adding a
little bit more than just your articles? I mean, what you say is valuable and everything.
Nevertheless think of if you added some great photos or video clips to give your posts more, “pop”!
Your content is excellent but with images and video clips, this website could definitely be one of the most
beneficial in its field. Awesome blog!
Byte arrays have this class:
byte[].class
Thanks
Thanks for this. This pointed me to the right way.
Really thx for that
Wonderful!!! I had this exact problem!!! Thank you soooo much!!!!