Dynamically load a class and call a method in Java

Suppose I want to dynamically load a class in java and call its start() (has no parameters):

 Class<?> c = Class.forName("AbuseMe"); c.getMethod("start").invoke(c.newInstance()); 

Would this be a good / safe way to do this?

+4
source share
2 answers

Looks nice.

If you do a lot of reflection-related code, you can look at Apache Beanutils or Apache OGNL or something similar.

+1
source

Reflection is a very useful approach for working with a Java class at runtime; it can be used to load a Java class, call its methods, or analyze the class at runtime.

Try this example.

This will help you.

How to use reflection to call a Java method at runtime

thanks

+3
source

All Articles