[英]How to call a method from loaded class using classLoader?
这是我使用的代码:
File urlclasspath = new File("C:/Users/ASUS/Desktop/semantics/semantics/bin");
URL urlarray[] = new URL[1];
urlarray[0] = urlclasspath.toURI().toURL();
MyClassLoader mycl = new MyClassLoader(urlarray);
Class myclass = mycl.loadClass("USAGE");
Object obj = myclass.newInstance();
我正在加载的类是USAGE ,我想调用的方法是main(String [] args)
您不需要调用newInstance()
。 做这个:
Class<?> myclass = mycl.loadClass("USAGE"); // get the class
Method m = myclass.getMethod("main", String[].class); // get the method you want to call
String[] args = new String[0]; // the arguments. Change this if you want to pass different args
m.invoke(null, args); // invoke the method
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.