package com.jong.test.reflect;
//第一个类
public class ReflectInvoke { /**
 * @param args
 */
public static void main(String[] args) {
System.out.println("It works!!");
}
public void print(){
System.out.println("print method!");
}
@Override
public String toString() {
return "ReflectInvoke";
}
}
//第2个类public class InvokeMainMethod { /**
 * @param args
 * @throws IllegalAccessException 
 * @throws InstantiationException 
 */
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
String className="com.jong.test.reflect.ReflectInvoke";
Class clazz=null;
try {
clazz=Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Object o=clazz.newInstance();
System.out.println(o.toString());
System.out.println(clazz.getClass());
ReflectInvoke.main(new String[]{});
Class[] arg=new Class[]{String.class};
//形式参数,要调用对象的形参
Object[] args1=new Object[]{};
//实参数,要调用对象的实参
InvokeMainMethod.method(o,"main");
try {
InvokeMainMethod.forname(o, "print", null,null);
} catch(Exception e){
System.out.println(e);
}
}
public static void forname(Object o,String methodname,Class[] arg,Object[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Class c=o.getClass();
Method m=c.getMethod(methodname,arg);//获得方法。
System.out.println(m.getName());
Object obj=m.invoke(o, args);
System.out.println(o.toString());
}
public static void method(Object o,String method){
Class c=o.getClass();
try {
Method m=c.getMethod(method, new Class[]{});
Object obj=m.invoke(o, null);
System.out.println(obj);
} catch (Exception e) {
System.out.println(e);
}    

}}
在第2个类里怎么调用第一个类的main方法,像第一个类的print方法是没有问题的,
关键是怎么获得String类型的数组?请教高手

解决方案 »

  1.   

    main方法是静态方法,跟调用其他类的static方法一样
    都是类名.main(args),args就是String类型的数组,从命令行传入的
    (如果用IDE的就自己去网上找看怎么输入,讲了太多次了不想讲了)
    还有就是以后代码别这样贴了,比较难看(我没看!)
      

  2.   

    我的问题是怎么样才能获得String类型的数组呢?package com.jong.test.reflect;import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    public class InvokeMainMethod { /**
     * @param args
     * @throws IllegalAccessException 
     * @throws InstantiationException 
     */
    public static void main(String[] args) throws InstantiationException, IllegalAccessException {
    String className="com.jong.test.reflect.ReflectInvoke";
    Class clazz=null;
    try {
    clazz=Class.forName(className);
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
    Object o=clazz.newInstance();
    System.out.println("========================"); System.out.println(o.toString());
    // clazz.getMethod("main", parameterTypes)
    System.out.println(clazz.getClass());
    System.out.println("========================"); ReflectInvoke.main(new String[]{});
    Class[] arg=new Class[]{};
    这里怎么获得String类型的数组????
             Object[] args1=new Object[]{};
    //实参数,要调用对象的实参
    InvokeMainMethod.method(o,"main");
    System.out.println("========================");
    try {
    InvokeMainMethod.forname(o, "print", null,null);
    System.out.println("========================");
    InvokeMainMethod.forname(o, "main",arg,args1);
    System.out.println("========================");
    InvokeMainMethod.invokeMethod(o, "main",new Object[]{"a","b"});
    InvokeMainMethod.invokeMethod(o, "main",null); System.out.println("========================");
    } catch(Exception e){
    System.out.println(e);
    }
    }
    public static Object invokeMethod(Object owner, String methodName, Object[] args) throws Exception {    
        Class ownerClass = owner.getClass();    
        Class[] argsClass = new Class[args.length];    
        for (int i = 0, j = args.length; i < j; i++) {    
            argsClass[i] = args[i].getClass();    
        }     
        Method method = ownerClass.getMethod(methodName, argsClass);       
        return method.invoke(owner, args);     
    }   }
      

  3.   

    同意此观点
    一般是不调用main 函数
    因为它是主函数
    如果要调用,上面的方法可以
    ^_*
      

  4.   

    try {
    ReflectInvoke.class.getMethod("main", new Class[]{String[].class}).invoke(null,new Object[]{null});
    // InvokeMainMethod.forname2(o, "main");
    } catch(Exception e){
    System.out.println(e);
    }
    可以这样做了...Class clazz=new Class[]{String[].class};这里没想到有String[]可以代表...String 数组...4555555555555