I have to get the class name from user and load it dynamically.
public class sample{
public static void main(String[] args) {
if(args.length < 1)
{
print_usage();
}
else{
Class inputClass = null;
try {
inputClass = Class.forName(args[0]);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
I have classes named Sample2, Sample3:
public class Sample2 {
private String name;
}
public class Sample3 {
private int value;
}
I want to load class based on user input, either Sample2/Sample3 class.
I have the files in the same directory, but I get the java.lang.ClassNotFoundException error. How do I fix this error?