Java : Load class based on user input -
i have class name user , load 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 load class based on user input, either sample2/sample3 class.
i have files in same directory, java.lang.classnotfoundexception error. how fix error?
please more clear on question, assume typing in class names without package.
if they're in package, must use class.forname that.
class inputclass = null; try { inputclass = class.forname("my.package.myclass"); } catch (classnotfoundexception e) { e.printstacktrace(); }
Comments
Post a Comment