Dynamically load a Class and invoke a method in Java

12,281

Solution 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.

Solution 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

Share:
12,281
lukuluku
Author by

lukuluku

Updated on June 04, 2022

Comments

  • lukuluku
    lukuluku almost 2 years

    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?