Problem description
The ClassNotFoundException occurs when a class cannot be found by using the Class.forName(java.lang.String) method. This exception can occur only during runtime.
Solution
The ClassNotFoundException occurs when a class cannot be found by using a string and the Class.forName(java.lang.String) method. This exception can occur only during runtime. Confirm that the class path is correct and properly referenced.
Sample code
package com.alibaba.mqc.test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
// Simple reflection demo
public class V {
public void printf(){
System.out.println("printf");
}
public static void main(String[] args) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class c = Class.forName("com.alibaba.mqc.test.V");
Method m = c.getDeclaredMethod("printf",null);
m.invoke(c.newInstance(),null);
}
}