I want to call the main method using reflection

For example, if you are developing a command line tool, you may want to test or debug the main method. Reflection is useful in such cases, but how can the main method be executed from reflection? The conclusion is the sample below.

try {
    Method mainMethod = Sample.class.getMethod("main", String[].class);
    String[] args = {"arg1", "arg2", "arg3"};
    mainMethod.invoke(null, (Object) args);
} catch (NoSuchMethodException 
            | SecurityException 
            | IllegalAccessException
            | IllegalArgumentException
            | InvocationTargetException e) {
    e.printStackTrace();
    throw new RuntimeException(e);
}

Is it ʻinvokeas a point? __ Givenull to the first argument, and specify ʻargs cast to ʻObjectfor the subsequent variable-length argument part. __ Let's protect these two points and have fun reflecting (´ ・ ω ・)

Recommended Posts

I want to call the main method using reflection
I want to call a method and count the number
I want to call a method of another class
I want to judge the range using the monthly degree
[Rough commentary] I want to marry the pluck method
[JDBC ③] I tried to input from the main method using placeholders and arguments.
I tried to explain the method
I want to expand the clickable part of the link_to method
I want to use the sanitize method other than View.
I wanted to add @VisibleForTesting to the method
I was addicted to the roll method
I want to display the number of orders for today using datetime.
I want to pass the argument of Annotation and the argument of the calling method to aspect
I want to output the day of the week
[Ruby] I want to do a method jump!
I want to var_dump the contents of the intent
When you want to use the method outside
I want to truncate after the decimal point
I want to get the value in Ruby
I want to create the strongest local development environment using VSCode Remote Containers
I want to separate the handling of call results according to the API caller (call trigger)
[Java] I want to calculate the difference from the date
I want to embed any TraceId in the log
I want to know the answer of the rock-paper-scissors app
I want to display the name of the poster of the comment
I want to dark mode with the SWT app
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
I want to simplify the log output on Android
I want to add a delete function to the comment function
Rspec: I want to test the post-execution state when I set a method on subject
I want to convert characters ...
[Beginner] I want to modify the migration file-How to use rollback-
[Rails] [bootstrap] I want to change the font size responsively
I want to use screen sharing on the login screen on Ubuntu 18
I want to build Java Applet without using an IDE
(´-`) .. oO (I want to easily find the standard output "Hello".
I want to bring Tomcat to the server and start the application
I want to change the log output settings of UtilLoggingJdbcLogger
I want to use the Java 8 DateTime API slowly (now)
I want to create a form to select the [Rails] category
I tried to build the environment little by little using docker
I want to put the JDK on my Mac PC
I want to give a class name to the select attribute
I want to recursively search the class list under the package
I want to distinct the duplicated data with has_many through
I want to transition to the same screen in the saved state
I want to narrow down the display of docker ps
I want to return multiple return values for the input argument
[Ruby] I want to reverse the order of the hash table
I want to temporarily disable the swipe gesture of UIPageViewController
I want to pass the startup command to postgres with docker-compose.
I want to simplify the conditional if-else statement in Java
How to use the link_to method
I want to give edit and delete permissions only to the poster
How to use the include? method
[Metal] I tried to figure out the flow until rendering using Metal
How to use the form_with method
I want to create a chat screen for the Swift chat app!
I want to understand the flow of Spring processing request parameters
Use Modifier # isStatic to determine if the [Reflection] method is static.