Using Java Reflection to invoke a method with Array Parameters

Looking for a date or love? Click here to find one for free!!

Find Singapore Apartment  and Room Rentals here

India Stock Market blog

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);

}

}

About yourmitra

I have started yourmitra on June 23rd 2007. This blog was started with the purpose to record the journey of my first web start-up yourmitra.com which has since then transformed into http://www.rentalandrealestate.com Now I write about anything under the sun in addition to sharing some information about my website.
This entry was posted in Technology and tagged , . Bookmark the permalink.

17 Responses to Using Java Reflection to invoke a method with Array Parameters

  1. William Chan says:

    Wonderful post! This was perfect in solving exactly this problem at work today.

  2. Brad says:

    This was a huge help to me. Thanks!

  3. pop says:

    but what about byte[] parameter ?
    it throws java.lang.IllegalArgumentException: argument type mismatch.
    any solution?

  4. Ehsan says:

    Many thanks, this helped me.

  5. guest says:

    This was really a great taught to post such a blocker.
    Thanks alot

  6. Yann says:

    Thanks, super useful !!

  7. punisher2000_ro@yahoo.com says:

    Perfect examples! Believe me, the Oracle docs for this matter are way more complex by way less useful as your examples! Good job!

  8. Punisher says:

    Please remove the email from my name, for my last post, if possible :)
    Entered it twice by mistake.

  9. Colm says:

    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 :)

  10. Tinto says:

    Great Soulution… Thank you….

  11. Madhes says:

    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!

  12. Guru says:

    excellent, the trick was to think about an array as an *Object :)

  13. Clockmaster says:

    Thanks, this really helped.
    How would I be able to figure this on my own?

  14. Abhishek says:

    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 ……!!

  15. Julien says:

    Thanks!

  16. 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!

  17. Kurt Huwig says:

    Byte arrays have this class:

    byte[].class

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s