Dynamically load a Class and invoke a method in Ja

2019-06-24 00:22发布

问题:

Lets say i want to dynamically load a class in java and call it's start() (has no params) method:

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

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

回答1:

Looks good to me.

If you're doing a lot of reflection-related code you might look at Apache Beanutils or Apache OGNL or something similar.



回答2:

Reflection is a very useful approach to deal with the Java class at runtime, it can be use to load the Java class, call its methods or analysis the class at runtime.

Try this example.

This will surely help you.

How To Use Reflection To Call Java Method At Runtime

Thanks