Notes for calling the main method in other classes through reflection in Java

Source: Internet
Author: User

 

 

1. Normally, we call methods of other classes. If the method is static, the class name is directly called. method; if not static: object. method; Because main is a static method, you only need to: Class Name. main (string... ARGs.

 

2. Use reflection to call the class. We pass in the class full name of the called class to the parameter in the main method of the called class, and then execute the main method according to the class name.

Public class invokeclassmainmethod {public static void main (string [] ARGs) throws exception {method startclassmainmethod = Class. forname (ARGs [0]). getmethod ("Main", string []. class); startclassmainmethod. invoke (null, new string [] {"duancanmeng1", "duancanmeng2", "duancanmeng3"}); // remarks }}

 

If you run the preceding code, the message "illegalargumentexception" is displayed. The message "Java. Lang. illegalargumentexception: Wrong number of arguments" is displayed. This indicates that the number of input method parameters is incorrect. The input parameter in the remarks section is: New String [] {"duancanmeng1", "duancanmeng2", "duancanmeng3"}, a string array, because the main method also needs to be passed in a string array, which seems to be correct on the surface. What is the problem?

We know that the main method parameter for starting the Java program is a string array. According to the jdk1.5 syntax, the entire array is a parameter. According to the jdk1.4 syntax, each element in the array corresponds to a parameter, when a string array is passed as a parameter to the invoke method, javac will process it according to the jdk1.4 syntax, because jdk1.5 must be compatible with the jdk1.4 syntax, that is, the array is split into several separate parameters, so the above exception will occur.

We know the reason, and it will solve the problem. Since the string array will be split into object parameters, we will put another coat on the outside of the string, when the package is split, only the layer outside is removed. The string array in the package can be passed as a separate parameter, as shown below:

startClassMainMethod.invoke(null, new Object[]{new String[]{"duancanmeng1","duancanmeng2","duancanmeng3"}});

Or, directly convert it into a parameter that is not an array, so that the program cannot be split up. The input is also a separate parameter, as shown below:

startClassMainMethod.invoke(null, (Object)new String[]{"duancanmeng1","duancanmeng2","duancanmeng3"});

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.