public class Test1 {
public static void main(String[] args) {
for (String arg : args) {
System.out.println(arg);
}
}
}
public class Test2 {
public static void main(String[] args) {
try {
//获取Test1字节码对象
Class<?> aClass = Class.forName("com.enjoy.study.framework.servlet.Test1");
Constructor<?> constructor = aClass.getConstructor();
Test1 test1 = (Test1) constructor.newInstance();
String arr [] = new String[]{"苹果","草莓","西瓜"};
//获取Test1类中的方法
Method method = aClass.getMethod("main",String[].class);
//通过invoke方法来调用,Test1中的方法
method.invoke(test1,new Object[]{arr});
} catch (Exception e) {
}
}
}