<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.12</version>
</dependency>
groovy
implementation 'org.reflections:reflections:0.9.12'
package com.example.dynamiccode;
public class DynamicCode {
public void execute() {
}
}
import org.reflections.Reflections;
public class Main {
public static void main(String[] args) throws Exception {
String packageName = "com.example.dynamiccode";
String className = "DynamicCode";
String methodName = "execute";
Reflections reflections = new Reflections(packageName);
Class<?> clazz = reflections.forName(className);
Object obj = clazz.getDeclaredConstructor().newInstance();
clazz.getMethod(methodName).invoke(obj);
}
}